blob: ab1061beeb34b33067a2545b4f1bb08e2e636f33 [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
Deepesh Garg366120f2022-03-01 11:56:20 +0530123 and is_cancelled = 0
Zarrare83ff382018-09-21 15:45:40 +0530124 order by posting_date desc limit 1
125 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
126
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530127 prev_gl_via_je = frappe.db.sql('''
128 SELECT p.name, p.posting_date FROM `tabJournal Entry` p, `tabJournal Entry Account` c
129 WHERE p.name = c.parent and p.company=%s and c.account=%s
130 and c.reference_type=%s and c.reference_name=%s
131 and c.reference_detail_no=%s and c.docstatus < 2 order by posting_date desc limit 1
132 ''', (doc.company, item.get(deferred_account), doc.doctype, doc.name, item.name), as_dict=True)
133
134 if prev_gl_via_je:
135 if (not prev_gl_entry) or (prev_gl_entry and
136 prev_gl_entry[0].posting_date < prev_gl_via_je[0].posting_date):
137 prev_gl_entry = prev_gl_via_je
138
Nabin Hait0a90ce52019-03-28 19:43:02 +0530139 if prev_gl_entry:
140 start_date = getdate(add_days(prev_gl_entry[0].posting_date, 1))
141 else:
142 start_date = item.service_start_date
Zarrare83ff382018-09-21 15:45:40 +0530143
Nabin Hait0a90ce52019-03-28 19:43:02 +0530144 end_date = get_last_day(start_date)
145 if end_date >= item.service_end_date:
146 end_date = item.service_end_date
147 last_gl_entry = True
Nabin Hait66d07c22019-03-29 13:25:11 +0530148 elif item.service_stop_date and end_date >= item.service_stop_date:
149 end_date = item.service_stop_date
150 last_gl_entry = True
Zarrare83ff382018-09-21 15:45:40 +0530151
Nabin Hait0a90ce52019-03-28 19:43:02 +0530152 if end_date > getdate(posting_date):
153 end_date = posting_date
Zarrare83ff382018-09-21 15:45:40 +0530154
Nabin Hait0a90ce52019-03-28 19:43:02 +0530155 if getdate(start_date) <= getdate(end_date):
156 return start_date, end_date, last_gl_entry
157 else:
158 return None, None, None
159
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530160def calculate_monthly_amount(doc, item, last_gl_entry, start_date, end_date, total_days, total_booking_days, account_currency):
161 amount, base_amount = 0, 0
Zarrare83ff382018-09-21 15:45:40 +0530162
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530163 if not last_gl_entry:
164 total_months = (item.service_end_date.year - item.service_start_date.year) * 12 + \
165 (item.service_end_date.month - item.service_start_date.month) + 1
166
167 prorate_factor = flt(date_diff(item.service_end_date, item.service_start_date)) \
168 / flt(date_diff(get_last_day(item.service_end_date), get_first_day(item.service_start_date)))
169
170 actual_months = rounded(total_months * prorate_factor, 1)
171
172 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
173 base_amount = flt(item.base_net_amount / actual_months, item.precision("base_net_amount"))
174
175 if base_amount + already_booked_amount > item.base_net_amount:
176 base_amount = item.base_net_amount - already_booked_amount
177
178 if account_currency==doc.company_currency:
179 amount = base_amount
180 else:
181 amount = flt(item.net_amount/actual_months, item.precision("net_amount"))
182 if amount + already_booked_amount_in_account_currency > item.net_amount:
183 amount = item.net_amount - already_booked_amount_in_account_currency
184
185 if not (get_first_day(start_date) == start_date and get_last_day(end_date) == end_date):
186 partial_month = flt(date_diff(end_date, start_date)) \
187 / flt(date_diff(get_last_day(end_date), get_first_day(start_date)))
188
189 base_amount = rounded(partial_month, 1) * base_amount
190 amount = rounded(partial_month, 1) * amount
191 else:
192 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
193 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
194 if account_currency==doc.company_currency:
195 amount = base_amount
196 else:
197 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
198
199 return amount, base_amount
200
201def calculate_amount(doc, item, last_gl_entry, total_days, total_booking_days, account_currency):
Zarrare83ff382018-09-21 15:45:40 +0530202 amount, base_amount = 0, 0
203 if not last_gl_entry:
204 base_amount = flt(item.base_net_amount*total_booking_days/flt(total_days), item.precision("base_net_amount"))
205 if account_currency==doc.company_currency:
206 amount = base_amount
207 else:
208 amount = flt(item.net_amount*total_booking_days/flt(total_days), item.precision("net_amount"))
209 else:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530210 already_booked_amount, already_booked_amount_in_account_currency = get_already_booked_amount(doc, item)
211
Zarrare83ff382018-09-21 15:45:40 +0530212 base_amount = flt(item.base_net_amount - already_booked_amount, item.precision("base_net_amount"))
213 if account_currency==doc.company_currency:
214 amount = base_amount
215 else:
Zarrare83ff382018-09-21 15:45:40 +0530216 amount = flt(item.net_amount - already_booked_amount_in_account_currency, item.precision("net_amount"))
217
218 return amount, base_amount
219
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530220def get_already_booked_amount(doc, item):
221 if doc.doctype == "Sales Invoice":
222 total_credit_debit, total_credit_debit_currency = "debit", "debit_in_account_currency"
223 deferred_account = "deferred_revenue_account"
224 else:
225 total_credit_debit, total_credit_debit_currency = "credit", "credit_in_account_currency"
226 deferred_account = "deferred_expense_account"
227
228 gl_entries_details = frappe.db.sql('''
229 select sum({0}) as total_credit, sum({1}) as total_credit_in_account_currency, voucher_detail_no
230 from `tabGL Entry` where company=%s and account=%s and voucher_type=%s and voucher_no=%s and voucher_detail_no=%s
Deepesh Garg366120f2022-03-01 11:56:20 +0530231 and is_cancelled = 0
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530232 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 Garg30a647f2022-01-07 19:52:38 +0530259 accounts_frozen_upto = frappe.get_cached_value('Accounts Settings', 'None', 'acc_frozen_upto')
260
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530261 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 +0530262 start_date, end_date, last_gl_entry = get_booking_dates(doc, item, posting_date=posting_date)
263 if not (start_date and end_date): return
Zarrare83ff382018-09-21 15:45:40 +0530264
Deepesh Garg98f294a2022-01-03 19:40:47 +0530265 account_currency = get_account_currency(item.expense_account or item.income_account)
Zarrare83ff382018-09-21 15:45:40 +0530266 if doc.doctype == "Sales Invoice":
267 against, project = doc.customer, doc.project
268 credit_account, debit_account = item.income_account, item.deferred_revenue_account
269 else:
270 against, project = doc.supplier, item.project
271 credit_account, debit_account = item.deferred_expense_account, item.expense_account
272
Nabin Hait0a90ce52019-03-28 19:43:02 +0530273 total_days = date_diff(item.service_end_date, item.service_start_date) + 1
274 total_booking_days = date_diff(end_date, start_date) + 1
275
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530276 if book_deferred_entries_based_on == 'Months':
277 amount, base_amount = calculate_monthly_amount(doc, item, last_gl_entry,
278 start_date, end_date, total_days, total_booking_days, account_currency)
279 else:
280 amount, base_amount = calculate_amount(doc, item, last_gl_entry,
281 total_days, total_booking_days, account_currency)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530282
Deepesh Gargf79a72d2021-06-24 14:14:46 +0530283 if not amount:
284 return
285
Deepesh Garg30a647f2022-01-07 19:52:38 +0530286 # check if books nor frozen till endate:
Deepesh Garg366120f2022-03-01 11:56:20 +0530287 if accounts_frozen_upto and (end_date) <= getdate(accounts_frozen_upto):
Deepesh Garg30a647f2022-01-07 19:52:38 +0530288 end_date = get_last_day(add_days(accounts_frozen_upto, 1))
289
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530290 if via_journal_entry:
291 book_revenue_via_journal_entry(doc, credit_account, debit_account, against, amount,
292 base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process, submit_journal_entry)
293 else:
294 make_gl_entries(doc, credit_account, debit_account, against,
295 amount, base_amount, end_date, project, account_currency, item.cost_center, item, deferred_process)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530296
297 # Returned in case of any errors because it tries to submit the same record again and again in case of errors
298 if frappe.flags.deferred_accounting_error:
299 return
Nabin Hait0a90ce52019-03-28 19:43:02 +0530300
301 if getdate(end_date) < getdate(posting_date) and not last_gl_entry:
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
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530304 via_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_via_journal_entry'))
305 submit_journal_entry = cint(frappe.db.get_singles_value('Accounts Settings', 'submit_journal_entries'))
306 book_deferred_entries_based_on = frappe.db.get_singles_value('Accounts Settings', 'book_deferred_entries_based_on')
Nabin Hait0a90ce52019-03-28 19:43:02 +0530307
308 for item in doc.get('items'):
309 if item.get(enable_check):
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530310 _book_deferred_revenue_or_expense(item, via_journal_entry, submit_journal_entry, book_deferred_entries_based_on)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530311
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530312def process_deferred_accounting(posting_date=None):
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530313 ''' Converts deferred income/expense into income/expense
314 Executed via background jobs on every month end '''
315
Chinmay D. Pai0d147b02020-05-24 00:54:04 +0530316 if not posting_date:
317 posting_date = today()
318
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530319 if not cint(frappe.db.get_singles_value('Accounts Settings', 'automatically_process_deferred_accounting_entry')):
320 return
321
322 start_date = add_months(today(), -1)
323 end_date = add_days(today(), -1)
324
Deepesh Garg50690952021-07-01 09:31:31 +0530325 companies = frappe.get_all('Company')
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530326
Deepesh Garg50690952021-07-01 09:31:31 +0530327 for company in companies:
328 for record_type in ('Income', 'Expense'):
329 doc = frappe.get_doc(dict(
330 doctype='Process Deferred Accounting',
331 company=company.name,
332 posting_date=posting_date,
333 start_date=start_date,
334 end_date=end_date,
335 type=record_type
336 ))
337
338 doc.insert()
339 doc.submit()
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530340
Nabin Hait0a90ce52019-03-28 19:43:02 +0530341def make_gl_entries(doc, credit_account, debit_account, against,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530342 amount, base_amount, posting_date, project, account_currency, cost_center, item, deferred_process=None):
Nabin Hait0a90ce52019-03-28 19:43:02 +0530343 # GL Entry for crediting the amount in the deferred expense
344 from erpnext.accounts.general_ledger import make_gl_entries
345
rohitwaghchauree123ec62019-11-06 15:25:00 +0530346 if amount == 0: return
347
Nabin Hait0a90ce52019-03-28 19:43:02 +0530348 gl_entries = []
349 gl_entries.append(
350 doc.get_gl_dict({
351 "account": credit_account,
352 "against": against,
353 "credit": base_amount,
354 "credit_in_account_currency": amount,
355 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530356 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530357 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530358 'project': project,
359 'against_voucher_type': 'Process Deferred Accounting',
360 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530361 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530362 )
363 # GL Entry to debit the amount from the expense
364 gl_entries.append(
365 doc.get_gl_dict({
366 "account": debit_account,
367 "against": against,
368 "debit": base_amount,
369 "debit_in_account_currency": amount,
370 "cost_center": cost_center,
Deepesh Garg7d61c032020-05-15 12:58:48 +0530371 "voucher_detail_no": item.name,
Nabin Hait0a90ce52019-03-28 19:43:02 +0530372 'posting_date': posting_date,
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530373 'project': project,
374 'against_voucher_type': 'Process Deferred Accounting',
375 'against_voucher': deferred_process
Deepesh Garg7d61c032020-05-15 12:58:48 +0530376 }, account_currency, item=item)
Nabin Hait0a90ce52019-03-28 19:43:02 +0530377 )
378
Zarrare83ff382018-09-21 15:45:40 +0530379 if gl_entries:
Nabin Hait29fcb142019-02-13 17:18:12 +0530380 try:
381 make_gl_entries(gl_entries, cancel=(doc.docstatus == 2), merge_entries=True)
382 frappe.db.commit()
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530383 except Exception as e:
384 if frappe.flags.in_test:
Deepesh Garg67a001d2021-12-04 19:19:03 +0530385 traceback = frappe.get_traceback()
386 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Garg3c64e202021-12-04 20:05:37 +0530387 raise e
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530388 else:
389 frappe.db.rollback()
390 traceback = frappe.get_traceback()
Deepesh Garg67a001d2021-12-04 19:19:03 +0530391 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Gargf1a669c2021-09-29 22:26:33 +0530392 frappe.flags.deferred_accounting_error = True
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530393
394def send_mail(deferred_process):
Ankush Menatb6783b12021-05-17 17:06:12 +0530395 title = _("Error while processing deferred accounting for {0}").format(deferred_process)
396 link = get_link_to_form('Process Deferred Accounting', deferred_process)
397 content = _("Deferred accounting failed for some invoices:") + "\n"
398 content += _("Please check Process Deferred Accounting {0} and submit manually after resolving errors.").format(link)
Mangesh-Khairnar2f7861a2020-05-02 20:09:33 +0530399 sendmail_to_system_managers(title, content)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530400
401def book_revenue_via_journal_entry(doc, credit_account, debit_account, against,
402 amount, base_amount, posting_date, project, account_currency, cost_center, item,
403 deferred_process=None, submit='No'):
404
405 if amount == 0: return
406
407 journal_entry = frappe.new_doc('Journal Entry')
408 journal_entry.posting_date = posting_date
409 journal_entry.company = doc.company
410 journal_entry.voucher_type = 'Deferred Revenue' if doc.doctype == 'Sales Invoice' \
411 else 'Deferred Expense'
412
413 debit_entry = {
414 'account': credit_account,
415 'credit': base_amount,
416 'credit_in_account_currency': amount,
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530417 'account_currency': account_currency,
418 'reference_name': doc.name,
419 'reference_type': doc.doctype,
420 'reference_detail_no': item.name,
421 'cost_center': cost_center,
422 'project': project,
423 }
424
425 credit_entry = {
426 'account': debit_account,
427 'debit': base_amount,
428 'debit_in_account_currency': amount,
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530429 'account_currency': account_currency,
430 'reference_name': doc.name,
431 'reference_type': doc.doctype,
432 'reference_detail_no': item.name,
433 'cost_center': cost_center,
434 'project': project,
435 }
436
437 for dimension in get_accounting_dimensions():
438 debit_entry.update({
439 dimension: item.get(dimension)
440 })
441
442 credit_entry.update({
443 dimension: item.get(dimension)
444 })
445
446 journal_entry.append('accounts', debit_entry)
447 journal_entry.append('accounts', credit_entry)
448
449 try:
450 journal_entry.save()
451
452 if submit:
453 journal_entry.submit()
Deepesh Garg0ba4fce2021-12-04 19:25:44 +0530454
455 frappe.db.commit()
Ankush Menat694ae812021-09-01 14:40:56 +0530456 except Exception:
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530457 frappe.db.rollback()
458 traceback = frappe.get_traceback()
Deepesh Garg67a001d2021-12-04 19:19:03 +0530459 frappe.log_error(title=_('Error while processing deferred accounting for Invoice {0}').format(doc.name), message=traceback)
Deepesh Garg7cc1cf32020-06-23 09:57:56 +0530460
461 frappe.flags.deferred_accounting_error = True
462
463def get_deferred_booking_accounts(doctype, voucher_detail_no, dr_or_cr):
464
465 if doctype == 'Sales Invoice':
466 credit_account, debit_account = frappe.db.get_value('Sales Invoice Item', {'name': voucher_detail_no},
467 ['income_account', 'deferred_revenue_account'])
468 else:
469 credit_account, debit_account = frappe.db.get_value('Purchase Invoice Item', {'name': voucher_detail_no},
470 ['deferred_expense_account', 'expense_account'])
471
472 if dr_or_cr == 'Debit':
473 return debit_account
474 else:
475 return credit_account