blob: 0c81d83ed8e81fd9eb3e32bdef888e378335e8c7 [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:
Ankush Menatf3b3d812021-05-17 16:43:38 +053044 conditions += f"AND p.company = {frappe.db.escape(company)}"
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +053045
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 Gargf79a72d2021-06-24 14:14:46 +0530266 if not amount:
267 return
268
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530269 if via_journal_entry:
270 book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount,
271 base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry)
272 else:
273 make_gl_entries(doc, credit_account, debit_account, against,
274 amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530275
276 # Returned in case of any errors because it tries to submit the same record again and again in case of errors
277 if frappe.flags.deferred_accounting_error:
278 return
Nabin Hait0a90ce52019-03-28 19:43:02 +0530279
280 if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530281 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530282
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530283 via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry'))
284 submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries'))
285 book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on')
Nabin Hait0a90ce52019-03-28 19:43:02 +0530286
287 for item in doc.get('items'):
288 if item.get(enable_check):
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530289 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530290
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530291def process_deferred_accounting(posting_date=None):
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530292 ''' Converts deferred income/expense into income/expense
293 Executed via background jobs on every month end '''
294
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530295 if not posting_date:
296 posting_date = today()
297
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530298 if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')):
299 return
300
301 start_date = add_months(today(), -1)
302 end_date = add_days(today(), -1)
303
Deepesh Garg50690952021-07-01 09:31:31 +0530304 companies = frappe.get_all('Company')
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530305
Deepesh Garg50690952021-07-01 09:31:31 +0530306 for company in companies:
307 for record_type in ('Income', 'Expense'):
308 doc = frappe.get_doc(dict(
309 doctype='Process Deferred Accounting',
310 company=company.name,
311 posting_date=posting_date,
312 start_date=start_date,
313 end_date=end_date,
314 type=record_type
315 ))
316
317 doc.insert()
318 doc.submit()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530319
Nabin Hait0a90ce52019-03-28 19:43:02 +0530320def make_gl_entries(doc, credit_account, debit_account, against,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530321 amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530322 # GL Entry for crediting the amount in the deferred expense
323 from erpnext.accounts.general_ledger import make_gl_entries
324
rohitwaghchauree123ec62019-11-06 15:25:00 +0530325 if amount == 0: return
326
Nabin Hait0a90ce52019-03-28 19:43:02 +0530327 gl_entries = []
328 gl_entries.append(
329 doc.get_gl_dict({
330 "account": credit_account,
331 "against": against,
332 "credit": base_amount,
333 "credit_in_account_currency": amount,
334 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530335 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530336 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530337 'project': project,
338 'against_voucher_type': 'Process Deferred Accounting',
339 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530340 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530341 )
342 # GL Entry to debit the amount from the expense
343 gl_entries.append(
344 doc.get_gl_dict({
345 "account": debit_account,
346 "against": against,
347 "debit": base_amount,
348 "debit_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
Zarrare83ff382018-09-21 15:45:40 +0530358 if gl_entries:
Nabin Hait29fcb142019-02-13 17:18:12 +0530359 try:
360 make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
361 frappe.db.commit()
362 except:
363 frappe.db.rollback()
Nabin Hait0a90ce52019-03-28 19:43:02 +0530364 traceback = frappe.get_traceback()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530365 frappe.log_error(message=traceback)
366
367 frappe.flags.deferred_accounting_error = True
368
369def send_mail(deferred_process):
Ankush Menatb6783b12021-05-17 17:06:12 +0530370 title = _("Error while processing deferred accounting for {0}").format(deferred_process)
371 link = get_link_to_form('Process Deferred Accounting', deferred_process)
372 content = _("Deferred accounting failed for some invoices:") + "\n"
373 content += _("Please check Process Deferred Accounting {0} and submit manually after resolving errors.").format(link)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530374 sendmail_to_system_managers(title, content)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530375
376def book_revenue_via_journal_entry(doc, credit_account, debit_account, against,
377 amount, base_amount, posting_date, project, account_currency, cost_center, item,
378 deferred_process=None, submit='No'):
379
380 if amount == 0: return
381
382 journal_entry = frappe.new_doc('Journal Entry')
383 journal_entry.posting_date = posting_date
384 journal_entry.company = doc.company
385 journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \
386 else 'Deferred Expense'
387
388 debit_entry = {
389 'account': credit_account,
390 'credit': base_amount,
391 'credit_in_account_currency': amount,
392 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
393 'party': against,
394 'account_currency': account_currency,
395 'reference_name': doc.name,
396 'reference_type': doc.doctype,
397 'reference_detail_no': item.name,
398 'cost_center': cost_center,
399 'project': project,
400 }
401
402 credit_entry = {
403 'account': debit_account,
404 'debit': base_amount,
405 'debit_in_account_currency': amount,
406 'party_type': 'Customer' if doc.doctype == 'Sales Invoice' else 'Supplier',
407 'party': against,
408 'account_currency': account_currency,
409 'reference_name': doc.name,
410 'reference_type': doc.doctype,
411 'reference_detail_no': item.name,
412 'cost_center': cost_center,
413 'project': project,
414 }
415
416 for dimension in get_accounting_dimensions():
417 debit_entry.update({
418 dimension: item.get(dimension)
419 })
420
421 credit_entry.update({
422 dimension: item.get(dimension)
423 })
424
425 journal_entry.append('accounts', debit_entry)
426 journal_entry.append('accounts', credit_entry)
427
428 try:
429 journal_entry.save()
430
431 if submit:
432 journal_entry.submit()
433 except:
434 frappe.db.rollback()
435 traceback = frappe.get_traceback()
436 frappe.log_error(message=traceback)
437
438 frappe.flags.deferred_accounting_error = True
439
440def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
441
442 if doctype == 'Sales Invoice':
443 credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no},
444 ['income_account', 'deferred_revenue_account'])
445 else:
446 credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no},
447 ['deferred_expense_account', 'expense_account'])
448
449 if dr_or_cr == 'Debit':
450 return debit_account
451 else:
452 return credit_account