blob: 71957e67a3cb2022f7a4eaca699610aa8dbd59a1 [file] [log] [blame]
Zarrare83ff382018-09-21 15:45:40 +05301from __future__ import unicode_literals
2
3import frappe
4from frappe import _
Nabin Hait0a90ce52019-03-28 19:43:02 +05305from frappe.email import sendmail_to_system_managers
Chillar Anand915b3432021-09-02 16:44:59 +05306from frappe.utils import (
7 add_days,
8 add_months,
9 cint,
10 date_diff,
11 flt,
12 get_first_day,
13 get_last_day,
14 get_link_to_form,
15 getdate,
16 rounded,
17 today,
18)
19
20from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
21 get_accounting_dimensions,
22)
23from erpnext.accounts.utils import get_account_currency
24
Zarrare83ff382018-09-21 15:45:40 +053025
26def validate_service_stop_date(doc):
27 ''' Validates service_stop_date for Purchase Invoice and Sales Invoice '''
28
29 enable_check = "enable_deferred_revenue" \
30 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
31
32 old_stop_dates = {}
33 old_doc = frappe.db.get_all("{0} Item".format(doc.doctype),
34 {"parent": doc.name}, ["name", "service_stop_date"])
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:
40 if not item.get(enable_check): continue
41
42 if item.service_stop_date:
43 if date_diff(item.service_stop_date, item.service_start_date) < 0:
44 frappe.throw(_("Service Stop Date cannot be before Service Start Date"))
45
46 if date_diff(item.service_stop_date, item.service_end_date) > 0:
47 frappe.throw(_("Service Stop Date cannot be after Service End Date"))
48
Rohit Waghchaurec699b2a2018-09-24 11:57:48 +053049 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 +053050 frappe.throw(_("Cannot change Service Stop Date for item in row {0}").format(item.idx))
Zarrare83ff382018-09-21 15:45:40 +053051
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053052def build_conditions(process_type, account, company):
53 conditions=''
54 deferred_account = "item.deferred_revenue_account" if process_type=="Income" else "item.deferred_expense_account"
55
56 if account:
57 conditions += "AND %s='%s'"%(deferred_account, account)
58 elif company:
Ankush Menatf3b3d812021-05-17 16:43:38 +053059 conditions += f"AND p.company = {frappe.db.escape(company)}"
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053060
61 return conditions
62
63def convert_deferred_expense_to_expense(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053064 # 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 +053065
Nabin Hait0a90ce52019-03-28 19:43:02 +053066 if not start_date:
67 start_date = add_months(today(), -1)
68 if not end_date:
69 end_date = add_days(today(), -1)
70
Zarrare83ff382018-09-21 15:45:40 +053071 # check for the purchase invoice for which GL entries has to be done
72 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053073 select distinct item.parent
74 from `tabPurchase Invoice Item` item, `tabPurchase Invoice` p
75 where item.service_start_date<=%s and item.service_end_date>=%s
76 and item.enable_deferred_expense = 1 and item.parent=p.name
77 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
78 {0}
79 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +053080
81 # For each invoice, book deferred expense
82 for invoice in invoices:
83 doc = frappe.get_doc("Purchase Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053084 book_deferred_income_or_expense(doc, deferred_process, end_date)
Zarrare83ff382018-09-21 15:45:40 +053085
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053086 if frappe.flags.deferred_accounting_error:
87 send_mail(deferred_process)
88
89def convert_deferred_revenue_to_income(deferred_process, start_date=None, end_date=None, conditions=''):
Nabin Hait0a90ce52019-03-28 19:43:02 +053090 # 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 +053091
Nabin Hait0a90ce52019-03-28 19:43:02 +053092 if not start_date:
93 start_date = add_months(today(), -1)
94 if not end_date:
95 end_date = add_days(today(), -1)
96
Zarrare83ff382018-09-21 15:45:40 +053097 # check for the sales invoice for which GL entries has to be done
98 invoices = frappe.db.sql_list('''
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053099 select distinct item.parent
100 from `tabSales Invoice Item` item, `tabSales Invoice` p
101 where item.service_start_date<=%s and item.service_end_date>=%s
102 and item.enable_deferred_revenue = 1 and item.parent=p.name
103 and item.docstatus = 1 and ifnull(item.amount, 0) > 0
104 {0}
105 '''.format(conditions), (end_date, start_date)) #nosec
Zarrare83ff382018-09-21 15:45:40 +0530106
Zarrare83ff382018-09-21 15:45:40 +0530107 for invoice in invoices:
108 doc = frappe.get_doc("Sales Invoice", invoice)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530109 book_deferred_income_or_expense(doc, deferred_process, end_date)
110
111 if frappe.flags.deferred_accounting_error:
112 send_mail(deferred_process)
Zarrare83ff382018-09-21 15:45:40 +0530113
Nabin Hait0a90ce52019-03-28 19:43:02 +0530114def get_booking_dates(doc, item, posting_date=None):
115 if not posting_date:
116 posting_date = add_days(today(), -1)
117
118 last_gl_entry = False
119
Zarrare83ff382018-09-21 15:45:40 +0530120 deferred_account = "deferred_revenue_account" if doc.doctype=="Sales Invoice" else "deferred_expense_account"
Zarrare83ff382018-09-21 15:45:40 +0530121
122 prev_gl_entry = frappe.db.sql('''
123 select name, posting_date from `tabGL Entry` where company=%s and account=%s and
124 voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
125 order by posting_date desc limit 1
126 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
127
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530128 prev_gl_via_je = frappe.db.sql('''
129 SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c
130 WHERE p.name = c.parent and p.company=%s and c.account=%s
131 and c.reference_type=%s and c.reference_name=%s
132 and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1
133 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
134
135 if prev_gl_via_je:
136 if (not prev_gl_entry) or (prev_gl_entry and
137 prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date):
138 prev_gl_entry = prev_gl_via_je
139
Nabin Hait0a90ce52019-03-28 19:43:02 +0530140 if prev_gl_entry:
141 start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1))
142 else:
143 start_date = item.service_start_date
Zarrare83ff382018-09-21 15:45:40 +0530144
Nabin Hait0a90ce52019-03-28 19:43:02 +0530145 end_date = get_last_day(start_date)
146 if end_date >= item.service_end_date:
147 end_date = item.service_end_date
148 last_gl_entry = True
Nabin Hait66d07c22019-03-29 13:25:11 +0530149 elif item.service_stop_date and end_date >= item.service_stop_date:
150 end_date = item.service_stop_date
151 last_gl_entry = True
Zarrare83ff382018-09-21 15:45:40 +0530152
Nabin Hait0a90ce52019-03-28 19:43:02 +0530153 if end_date > getdate(posting_date):
154 end_date = posting_date
Zarrare83ff382018-09-21 15:45:40 +0530155
Nabin Hait0a90ce52019-03-28 19:43:02 +0530156 if getdate(start_date) <= getdate(end_date):
157 return start_date, end_date, last_gl_entry
158 else:
159 return None, None, None
160
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530161def calculate_monthly_amount(doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency):
162 amount, base_amount = 0, 0
Zarrare83ff382018-09-21 15:45:40 +0530163
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530164 if not last_gl_entry:
165 total_months = (item.service_end_date.year - item.service_start_date.year) * 12 + \
166 (item.service_end_date.month - item.service_start_date.month) + 1
167
168 prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) \
169 / flt(date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date)))
170
171 actual_months = rounded(total_months * prorate_factor, 1)
172
173 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
174 base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount"))
175
176 if base_amount + already_booked_amount > item.base_net_amount:
177 base_amount = item.base_net_amount - already_booked_amount
178
179 if account_currency==doc.company_currency:
180 amount = base_amount
181 else:
182 amount = flt(item.net_amount/actual_months, item.precision("net_amount"))
183 if amount + already_booked_amount_in_account_currency > item.net_amount:
184 amount = item.net_amount - already_booked_amount_in_account_currency
185
186 if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date):
187 partial_month = flt(date_diff(end_date, start_date)) \
188 / flt(date_diff(get_last_day(end_date), get_first_day(start_date)))
189
190 base_amount = rounded(partial_month, 1) * base_amount
191 amount = rounded(partial_month, 1) * amount
192 else:
193 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
194 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
195 if account_currency==doc.company_currency:
196 amount = base_amount
197 else:
198 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
199
200 return amount, base_amount
201
202def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency):
Zarrare83ff382018-09-21 15:45:40 +0530203 amount, base_amount = 0, 0
204 if not last_gl_entry:
205 base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount"))
206 if account_currency==doc.company_currency:
207 amount = base_amount
208 else:
209 amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount"))
210 else:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530211 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
212
Zarrare83ff382018-09-21 15:45:40 +0530213 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
214 if account_currency==doc.company_currency:
215 amount = base_amount
216 else:
Zarrare83ff382018-09-21 15:45:40 +0530217 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
218
219 return amount, base_amount
220
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530221def get_already_booked_amount(doc, item):
222 if doc.doctype == "Sales Invoice":
223 total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency"
224 deferred_account = "deferred_revenue_account"
225 else:
226 total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency"
227 deferred_account = "deferred_expense_account"
228
229 gl_entries_details = frappe.db.sql('''
230 select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no
231 from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
232 group by voucher_detail_no
233 '''.format(total_credit_debit, total_credit_debit_currency),
234 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
235
236 journal_entry_details = frappe.db.sql('''
237 SELECT sum(c.{0}) as total_credit, sum(c.{1}) as total_credit_in_account_currency, reference_detail_no
238 FROM `tabJournal Entry` p , `tabJournal Entry Account` c WHERE p.name = c.parent and
239 p.company = %s and c.account=%s and c.reference_type=%s and c.reference_name=%s and c.reference_detail_no=%s
240 and p.docstatus < 2 group by reference_detail_no
241 '''.format(total_credit_debit, total_credit_debit_currency),
242 (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
243
244 already_booked_amount = gl_entries_details[0].total_credit if gl_entries_details else 0
245 already_booked_amount += journal_entry_details[0].total_credit if journal_entry_details else 0
246
247 if doc.currency == doc.company_currency:
248 already_booked_amount_in_account_currency = already_booked_amount
249 else:
250 already_booked_amount_in_account_currency = gl_entries_details[0].total_credit_in_account_currency if gl_entries_details else 0
251 already_booked_amount_in_account_currency += journal_entry_details[0].total_credit_in_account_currency if journal_entry_details else 0
252
253 return already_booked_amount, already_booked_amount_in_account_currency
254
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530255def book_deferred_income_or_expense(doc, deferred_process, posting_date=None):
Nabin Hait27af6b32019-02-08 16:52:13 +0530256 enable_check = "enable_deferred_revenue" \
257 if doc.doctype=="Sales Invoice" else "enable_deferred_expense"
Zarrare83ff382018-09-21 15:45:40 +0530258
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530259 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 +0530260 start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date)
261 if not (start_date and end_date): return
Zarrare83ff382018-09-21 15:45:40 +0530262
263 account_currency = get_account_currency(item.expense_account)
Zarrare83ff382018-09-21 15:45:40 +0530264 if doc.doctype == "Sales Invoice":
265 against, project = doc.customer, doc.project
266 credit_account, debit_account = item.income_account, item.deferred_revenue_account
267 else:
268 against, project = doc.supplier, item.project
269 credit_account, debit_account = item.deferred_expense_account, item.expense_account
270
Nabin Hait0a90ce52019-03-28 19:43:02 +0530271 total_days = date_diff(item.service_end_date, item.service_start_date) + 1
272 total_booking_days = date_diff(end_date, start_date) + 1
273
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530274 if book_deferred_entries_based_on == 'Months':
275 amount, base_amount = calculate_monthly_amount(doc, item, last_gl_entry,
276 start_date, end_date, total_days, total_booking_days, account_currency)
277 else:
278 amount, base_amount = calculate_amount(doc, item, last_gl_entry,
279 total_days, total_booking_days, account_currency)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530280
Deepesh Gargf79a72d2021-06-24 14:14:46 +0530281 if not amount:
282 return
283
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530284 if via_journal_entry:
285 book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount,
286 base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry)
287 else:
288 make_gl_entries(doc, credit_account, debit_account, against,
289 amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530290
291 # Returned in case of any errors because it tries to submit the same record again and again in case of errors
292 if frappe.flags.deferred_accounting_error:
293 return
Nabin Hait0a90ce52019-03-28 19:43:02 +0530294
295 if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530296 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530297
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530298 via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry'))
299 submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries'))
300 book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on')
Nabin Hait0a90ce52019-03-28 19:43:02 +0530301
302 for item in doc.get('items'):
303 if item.get(enable_check):
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530304 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530305
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530306def process_deferred_accounting(posting_date=None):
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530307 ''' Converts deferred income/expense into income/expense
308 Executed via background jobs on every month end '''
309
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530310 if not posting_date:
311 posting_date = today()
312
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530313 if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')):
314 return
315
316 start_date = add_months(today(), -1)
317 end_date = add_days(today(), -1)
318
Deepesh Garg50690952021-07-01 09:31:31 +0530319 companies = frappe.get_all('Company')
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530320
Deepesh Garg50690952021-07-01 09:31:31 +0530321 for company in companies:
322 for record_type in ('Income', 'Expense'):
323 doc = frappe.get_doc(dict(
324 doctype='Process Deferred Accounting',
325 company=company.name,
326 posting_date=posting_date,
327 start_date=start_date,
328 end_date=end_date,
329 type=record_type
330 ))
331
332 doc.insert()
333 doc.submit()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530334
Nabin Hait0a90ce52019-03-28 19:43:02 +0530335def make_gl_entries(doc, credit_account, debit_account, against,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530336 amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530337 # GL Entry for crediting the amount in the deferred expense
338 from erpnext.accounts.general_ledger import make_gl_entries
339
rohitwaghchauree123ec62019-11-06 15:25:00 +0530340 if amount == 0: return
341
Nabin Hait0a90ce52019-03-28 19:43:02 +0530342 gl_entries = []
343 gl_entries.append(
344 doc.get_gl_dict({
345 "account": credit_account,
346 "against": against,
347 "credit": base_amount,
348 "credit_in_account_currency": amount,
349 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530350 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530351 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530352 'project': project,
353 'against_voucher_type': 'Process Deferred Accounting',
354 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530355 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530356 )
357 # GL Entry to debit the amount from the expense
358 gl_entries.append(
359 doc.get_gl_dict({
360 "account": debit_account,
361 "against": against,
362 "debit": base_amount,
363 "debit_in_account_currency": amount,
364 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530365 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530366 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530367 'project': project,
368 'against_voucher_type': 'Process Deferred Accounting',
369 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530370 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530371 )
372
Zarrare83ff382018-09-21 15:45:40 +0530373 if gl_entries:
Nabin Hait29fcb142019-02-13 17:18:12 +0530374 try:
375 make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
376 frappe.db.commit()
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530377 except Exception as e:
378 if frappe.flags.in_test:
379 raise e
380 else:
381 frappe.db.rollback()
382 traceback = frappe.get_traceback()
383 frappe.log_error(message=traceback)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530384
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530385 frappe.flags.deferred_accounting_error = True
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530386
387def send_mail(deferred_process):
Ankush Menatb6783b12021-05-17 17:06:12 +0530388 title = _("Error while processing deferred accounting for {0}").format(deferred_process)
389 link = get_link_to_form('Process Deferred Accounting', deferred_process)
390 content = _("Deferred accounting failed for some invoices:") + "\n"
391 content += _("Please check Process Deferred Accounting {0} and submit manually after resolving errors.").format(link)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530392 sendmail_to_system_managers(title, content)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530393
394def book_revenue_via_journal_entry(doc, credit_account, debit_account, against,
395 amount, base_amount, posting_date, project, account_currency, cost_center, item,
396 deferred_process=None, submit='No'):
397
398 if amount == 0: return
399
400 journal_entry = frappe.new_doc('Journal Entry')
401 journal_entry.posting_date = posting_date
402 journal_entry.company = doc.company
403 journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \
404 else 'Deferred Expense'
405
406 debit_entry = {
407 'account': credit_account,
408 'credit': base_amount,
409 'credit_in_account_currency': amount,
410 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
411 'party': against,
412 'account_currency': account_currency,
413 'reference_name': doc.name,
414 'reference_type': doc.doctype,
415 'reference_detail_no': item.name,
416 'cost_center': cost_center,
417 'project': project,
418 }
419
420 credit_entry = {
421 'account': debit_account,
422 'debit': base_amount,
423 'debit_in_account_currency': amount,
424 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
425 'party': against,
426 'account_currency': account_currency,
427 'reference_name': doc.name,
428 'reference_type': doc.doctype,
429 'reference_detail_no': item.name,
430 'cost_center': cost_center,
431 'project': project,
432 }
433
434 for dimension in get_accounting_dimensions():
435 debit_entry.update({
436 dimension: item.get(dimension)
437 })
438
439 credit_entry.update({
440 dimension: item.get(dimension)
441 })
442
443 journal_entry.append('accounts', debit_entry)
444 journal_entry.append('accounts', credit_entry)
445
446 try:
447 journal_entry.save()
448
449 if submit:
450 journal_entry.submit()
Ankush Menat694ae812021-09-01 14:40:56 +0530451 except Exception:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530452 frappe.db.rollback()
453 traceback = frappe.get_traceback()
454 frappe.log_error(message=traceback)
455
456 frappe.flags.deferred_accounting_error = True
457
458def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
459
460 if doctype == 'Sales Invoice':
461 credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no},
462 ['income_account', 'deferred_revenue_account'])
463 else:
464 credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no},
465 ['deferred_expense_account', 'expense_account'])
466
467 if dr_or_cr == 'Debit':
468 return debit_account
469 else:
470 return credit_account