blob: 22c81ddd46396222060c702ad9aea9f035fd9525 [file] [log] [blame]
Zarrare83ff382018-09-21 15:45:40 +05301import frappe
2from frappe import _
Nabin Hait0a90ce52019-03-28 19:43:02 +05303from frappe.email import sendmail_to_system_managers
Chillar Anand915b3432021-09-02 16:44:59 +05304from 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
18from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
19 get_accounting_dimensions,
20)
21from erpnext.accounts.utils import get_account_currency
22
Zarrare83ff382018-09-21 15:45:40 +053023
24def validate_service_stop_date(doc):
25 ''' Validates service_stop_date for Purchase Invoice and Sales Invoice '''
26
27 enable_check = "enable_deferred_revenue" \
28 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
29
30 old_stop_dates = {}
31 old_doc = frappe.db.get_all("{0} Item".format(doc.doctype),
32 {"parent": doc.name}, ["name", "service_stop_date"])
33
34 for d in old_doc:
35 old_stop_dates[d.name] = d.service_stop_date or ""
36
37 for item in doc.items:
38 if not item.get(enable_check): continue
39
40 if item.service_stop_date:
41 if date_diff(item.service_stop_date, item.service_start_date) < 0:
42 frappe.throw(_("Service Stop Date cannot be before Service Start Date"))
43
44 if date_diff(item.service_stop_date, item.service_end_date) > 0:
45 frappe.throw(_("Service Stop Date cannot be after Service End Date"))
46
Rohit Waghchaurec699b2a2018-09-24 11:57:48 +053047 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 +053048 frappe.throw(_("Cannot change Service Stop Date for item in row {0}").format(item.idx))
Zarrare83ff382018-09-21 15:45:40 +053049
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053050def build_conditions(process_type, account, company):
51 conditions=''
52 deferred_account = "item.deferred_revenue_account" if process_type=="Income" else "item.deferred_expense_account"
53
54 if account:
55 conditions += "AND %s='%s'"%(deferred_account, account)
56 elif company:
Ankush Menatf3b3d812021-05-17 16:43:38 +053057 conditions += f"AND p.company = {frappe.db.escape(company)}"
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053058
59 return conditions
60
61def convert_deferred_expense_to_expense(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053062 # 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 +053063
Nabin Hait0a90ce52019-03-28 19:43:02 +053064 if not start_date:
65 start_date = add_months(today(), -1)
66 if not end_date:
67 end_date = add_days(today(), -1)
68
Zarrare83ff382018-09-21 15:45:40 +053069 # check for the purchase invoice for which GL entries has to be done
70 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053071 select distinct item.parent
72 from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p
73 where item.service_start_date<=%s and item.service_end_date>=%s
74 and item.enable_deferred_expense = 1 and item.parent=p.name
75 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
76 {0}
77 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +053078
79 # For each invoice, book deferred expense
80 for invoice in invoices:
81 doc = frappe.get_doc("Purchase Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053082 book_deferred_income_or_expense(doc, deferred_process, end_date)
Zarrare83ff382018-09-21 15:45:40 +053083
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053084 if frappe.flags.deferred_accounting_error:
85 send_mail(deferred_process)
86
87def convert_deferred_revenue_to_income(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053088 # 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 +053089
Nabin Hait0a90ce52019-03-28 19:43:02 +053090 if not start_date:
91 start_date = add_months(today(), -1)
92 if not end_date:
93 end_date = add_days(today(), -1)
94
Zarrare83ff382018-09-21 15:45:40 +053095 # check for the sales invoice for which GL entries has to be done
96 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053097 select distinct item.parent
98 from `tabSales Invoice Item` item, `tabSales Invoice` p
99 where item.service_start_date<=%s and item.service_end_date>=%s
100 and item.enable_deferred_revenue = 1 and item.parent=p.name
101 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
102 {0}
103 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +0530104
Zarrare83ff382018-09-21 15:45:40 +0530105 for invoice in invoices:
106 doc = frappe.get_doc("Sales Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530107 book_deferred_income_or_expense(doc, deferred_process, end_date)
108
109 if frappe.flags.deferred_accounting_error:
110 send_mail(deferred_process)
Zarrare83ff382018-09-21 15:45:40 +0530111
Nabin Hait0a90ce52019-03-28 19:43:02 +0530112def get_booking_dates(doc, item, posting_date=None):
113 if not posting_date:
114 posting_date = add_days(today(), -1)
115
116 last_gl_entry = False
117
Zarrare83ff382018-09-21 15:45:40 +0530118 deferred_account = "deferred_revenue_account" if doc.doctype=="Sales Invoice" else "deferred_expense_account"
Zarrare83ff382018-09-21 15:45:40 +0530119
120 prev_gl_entry = frappe.db.sql('''
121 select name, posting_date from `tabGL Entry` where company=%s and account=%s and
122 voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
123 order by posting_date desc limit 1
124 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
125
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530126 prev_gl_via_je = frappe.db.sql('''
127 SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c
128 WHERE p.name = c.parent and p.company=%s and c.account=%s
129 and c.reference_type=%s and c.reference_name=%s
130 and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1
131 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
132
133 if prev_gl_via_je:
134 if (not prev_gl_entry) or (prev_gl_entry and
135 prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date):
136 prev_gl_entry = prev_gl_via_je
137
Nabin Hait0a90ce52019-03-28 19:43:02 +0530138 if prev_gl_entry:
139 start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1))
140 else:
141 start_date = item.service_start_date
Zarrare83ff382018-09-21 15:45:40 +0530142
Nabin Hait0a90ce52019-03-28 19:43:02 +0530143 end_date = get_last_day(start_date)
144 if end_date >= item.service_end_date:
145 end_date = item.service_end_date
146 last_gl_entry = True
Nabin Hait66d07c22019-03-29 13:25:11 +0530147 elif item.service_stop_date and end_date >= item.service_stop_date:
148 end_date = item.service_stop_date
149 last_gl_entry = True
Zarrare83ff382018-09-21 15:45:40 +0530150
Nabin Hait0a90ce52019-03-28 19:43:02 +0530151 if end_date > getdate(posting_date):
152 end_date = posting_date
Zarrare83ff382018-09-21 15:45:40 +0530153
Nabin Hait0a90ce52019-03-28 19:43:02 +0530154 if getdate(start_date) <= getdate(end_date):
155 return start_date, end_date, last_gl_entry
156 else:
157 return None, None, None
158
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530159def calculate_monthly_amount(doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency):
160 amount, base_amount = 0, 0
Zarrare83ff382018-09-21 15:45:40 +0530161
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530162 if not last_gl_entry:
163 total_months = (item.service_end_date.year - item.service_start_date.year) * 12 + \
164 (item.service_end_date.month - item.service_start_date.month) + 1
165
166 prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) \
167 / flt(date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date)))
168
169 actual_months = rounded(total_months * prorate_factor, 1)
170
171 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
172 base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount"))
173
174 if base_amount + already_booked_amount > item.base_net_amount:
175 base_amount = item.base_net_amount - already_booked_amount
176
177 if account_currency==doc.company_currency:
178 amount = base_amount
179 else:
180 amount = flt(item.net_amount/actual_months, item.precision("net_amount"))
181 if amount + already_booked_amount_in_account_currency > item.net_amount:
182 amount = item.net_amount - already_booked_amount_in_account_currency
183
184 if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date):
185 partial_month = flt(date_diff(end_date, start_date)) \
186 / flt(date_diff(get_last_day(end_date), get_first_day(start_date)))
187
188 base_amount = rounded(partial_month, 1) * base_amount
189 amount = rounded(partial_month, 1) * amount
190 else:
191 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
192 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
193 if account_currency==doc.company_currency:
194 amount = base_amount
195 else:
196 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
197
198 return amount, base_amount
199
200def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency):
Zarrare83ff382018-09-21 15:45:40 +0530201 amount, base_amount = 0, 0
202 if not last_gl_entry:
203 base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount"))
204 if account_currency==doc.company_currency:
205 amount = base_amount
206 else:
207 amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount"))
208 else:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530209 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
210
Zarrare83ff382018-09-21 15:45:40 +0530211 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
212 if account_currency==doc.company_currency:
213 amount = base_amount
214 else:
Zarrare83ff382018-09-21 15:45:40 +0530215 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
216
217 return amount, base_amount
218
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530219def get_already_booked_amount(doc, item):
220 if doc.doctype == "Sales Invoice":
221 total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency"
222 deferred_account = "deferred_revenue_account"
223 else:
224 total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency"
225 deferred_account = "deferred_expense_account"
226
227 gl_entries_details = frappe.db.sql('''
228 select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no
229 from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
230 group by voucher_detail_no
231 '''.format(total_credit_debit, total_credit_debit_currency),
232 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
233
234 journal_entry_details = frappe.db.sql('''
235 SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no
236 FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and
237 p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s
238 and p.docstatus < 2 group by reference_detail_no
239 '''.format(total_credit_debit, total_credit_debit_currency),
240 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
241
242 already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0
243 already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0
244
245 if doc.currency == doc.company_currency:
246 already_booked_amount_in_account_currency = already_booked_amount
247 else:
248 already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0
249 already_booked_amount_in_account_currency += journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0
250
251 return already_booked_amount, already_booked_amount_in_account_currency
252
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530253def book_deferred_income_or_expense(doc, deferred_process, posting_date=None):
Nabin Hait27af6b32019-02-08 16:52:13 +0530254 enable_check = "enable_deferred_revenue" \
255 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
Zarrare83ff382018-09-21 15:45:40 +0530256
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530257 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 +0530258 start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date)
259 if not (start_date and end_date): return
Zarrare83ff382018-09-21 15:45:40 +0530260
261 account_currency = get_account_currency(item.expense_account)
Zarrare83ff382018-09-21 15:45:40 +0530262 if doc.doctype == "Sales Invoice":
263 against, project = doc.customer, doc.project
264 credit_account, debit_account = item.income_account, item.deferred_revenue_account
265 else:
266 against, project = doc.supplier, item.project
267 credit_account, debit_account = item.deferred_expense_account, item.expense_account
268
Nabin Hait0a90ce52019-03-28 19:43:02 +0530269 total_days = date_diff(item.service_end_date, item.service_start_date) + 1
270 total_booking_days = date_diff(end_date, start_date) + 1
271
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530272 if book_deferred_entries_based_on == 'Months':
273 amount, base_amount = calculate_monthly_amount(doc, item, last_gl_entry,
274 start_date, end_date, total_days, total_booking_days, account_currency)
275 else:
276 amount, base_amount = calculate_amount(doc, item, last_gl_entry,
277 total_days, total_booking_days, account_currency)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530278
Deepesh Gargf79a72d2021-06-24 14:14:46 +0530279 if not amount:
280 return
281
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530282 if via_journal_entry:
283 book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount,
284 base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry)
285 else:
286 make_gl_entries(doc, credit_account, debit_account, against,
287 amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530288
289 # Returned in case of any errors because it tries to submit the same record again and again in case of errors
290 if frappe.flags.deferred_accounting_error:
291 return
Nabin Hait0a90ce52019-03-28 19:43:02 +0530292
293 if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530294 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530295
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530296 via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry'))
297 submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries'))
298 book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on')
Nabin Hait0a90ce52019-03-28 19:43:02 +0530299
300 for item in doc.get('items'):
301 if item.get(enable_check):
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530302 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530303
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530304def process_deferred_accounting(posting_date=None):
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530305 ''' Converts deferred income/expense into income/expense
306 Executed via background jobs on every month end '''
307
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530308 if not posting_date:
309 posting_date = today()
310
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530311 if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')):
312 return
313
314 start_date = add_months(today(), -1)
315 end_date = add_days(today(), -1)
316
Deepesh Garg50690952021-07-01 09:31:31 +0530317 companies = frappe.get_all('Company')
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530318
Deepesh Garg50690952021-07-01 09:31:31 +0530319 for company in companies:
320 for record_type in ('Income', 'Expense'):
321 doc = frappe.get_doc(dict(
322 doctype='Process Deferred Accounting',
323 company=company.name,
324 posting_date=posting_date,
325 start_date=start_date,
326 end_date=end_date,
327 type=record_type
328 ))
329
330 doc.insert()
331 doc.submit()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530332
Nabin Hait0a90ce52019-03-28 19:43:02 +0530333def make_gl_entries(doc, credit_account, debit_account, against,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530334 amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530335 # GL Entry for crediting the amount in the deferred expense
336 from erpnext.accounts.general_ledger import make_gl_entries
337
rohitwaghchauree123ec62019-11-06 15:25:00 +0530338 if amount == 0: return
339
Nabin Hait0a90ce52019-03-28 19:43:02 +0530340 gl_entries = []
341 gl_entries.append(
342 doc.get_gl_dict({
343 "account": credit_account,
344 "against": against,
345 "credit": base_amount,
346 "credit_in_account_currency": amount,
347 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530348 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530349 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530350 'project': project,
351 'against_voucher_type': 'Process Deferred Accounting',
352 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530353 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530354 )
355 # GL Entry to debit the amount from the expense
356 gl_entries.append(
357 doc.get_gl_dict({
358 "account": debit_account,
359 "against": against,
360 "debit": base_amount,
361 "debit_in_account_currency": amount,
362 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530363 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530364 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530365 'project': project,
366 'against_voucher_type': 'Process Deferred Accounting',
367 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530368 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530369 )
370
Zarrare83ff382018-09-21 15:45:40 +0530371 if gl_entries:
Nabin Hait29fcb142019-02-13 17:18:12 +0530372 try:
373 make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
374 frappe.db.commit()
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530375 except Exception as e:
376 if frappe.flags.in_test:
Deepesh Garg67a001d2021-12-04 19:19:03 +0530377 traceback = frappe.get_traceback()
378 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Garg3c64e202021-12-04 20:05:37 +0530379 raise e
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530380 else:
381 frappe.db.rollback()
382 traceback = frappe.get_traceback()
Deepesh Garg67a001d2021-12-04 19:19:03 +0530383 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530384 frappe.flags.deferred_accounting_error = True
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530385
386def send_mail(deferred_process):
Ankush Menatb6783b12021-05-17 17:06:12 +0530387 title = _("Error while processing deferred accounting for {0}").format(deferred_process)
388 link = get_link_to_form('Process Deferred Accounting', deferred_process)
389 content = _("Deferred accounting failed for some invoices:") + "\n"
390 content += _("Please check Process Deferred Accounting {0} and submit manually after resolving errors.").format(link)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530391 sendmail_to_system_managers(title, content)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530392
393def book_revenue_via_journal_entry(doc, credit_account, debit_account, against,
394 amount, base_amount, posting_date, project, account_currency, cost_center, item,
395 deferred_process=None, submit='No'):
396
397 if amount == 0: return
398
399 journal_entry = frappe.new_doc('Journal Entry')
400 journal_entry.posting_date = posting_date
401 journal_entry.company = doc.company
402 journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \
403 else 'Deferred Expense'
404
405 debit_entry = {
406 'account': credit_account,
407 'credit': base_amount,
408 'credit_in_account_currency': amount,
409 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
410 'party': against,
411 'account_currency': account_currency,
412 'reference_name': doc.name,
413 'reference_type': doc.doctype,
414 'reference_detail_no': item.name,
415 'cost_center': cost_center,
416 'project': project,
417 }
418
419 credit_entry = {
420 'account': debit_account,
421 'debit': base_amount,
422 'debit_in_account_currency': amount,
423 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
424 'party': against,
425 'account_currency': account_currency,
426 'reference_name': doc.name,
427 'reference_type': doc.doctype,
428 'reference_detail_no': item.name,
429 'cost_center': cost_center,
430 'project': project,
431 }
432
433 for dimension in get_accounting_dimensions():
434 debit_entry.update({
435 dimension: item.get(dimension)
436 })
437
438 credit_entry.update({
439 dimension: item.get(dimension)
440 })
441
442 journal_entry.append('accounts', debit_entry)
443 journal_entry.append('accounts', credit_entry)
444
445 try:
446 journal_entry.save()
447
448 if submit:
449 journal_entry.submit()
Deepesh Garg0ba4fce2021-12-04 19:25:44 +0530450
451 frappe.db.commit()
Ankush Menat694ae812021-09-01 14:40:56 +0530452 except Exception:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530453 frappe.db.rollback()
454 traceback = frappe.get_traceback()
Deepesh Garg67a001d2021-12-04 19:19:03 +0530455 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530456
457 frappe.flags.deferred_accounting_error = True
458
459def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
460
461 if doctype == 'Sales Invoice':
462 credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no},
463 ['income_account', 'deferred_revenue_account'])
464 else:
465 credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no},
466 ['deferred_expense_account', 'expense_account'])
467
468 if dr_or_cr == 'Debit':
469 return debit_account
470 else:
471 return credit_account