deferred account in the item and company master

This commit is contained in:
Manas Solanki 2018-05-14 16:16:46 +05:30
parent a27d29b349
commit 0393848f60
6 changed files with 4745 additions and 4280 deletions

View File

@ -4,7 +4,7 @@
from __future__ import unicode_literals
import frappe, erpnext
import frappe.defaults
from frappe.utils import cint, flt
from frappe.utils import cint, flt, add_months, today, date_diff, getdate, add_days
from frappe import _, msgprint, throw
from erpnext.accounts.party import get_party_account, get_due_date
from erpnext.controllers.stock_controller import update_gl_entries_after
@ -695,7 +695,7 @@ class SalesInvoice(SellingController):
account_currency = get_account_currency(item.income_account)
gl_entries.append(
self.get_gl_dict({
"account": item.income_account,
"account": item.income_account if not item.enable_deferred_revenue else item.deferred_revenue_account,
"against": self.customer,
"credit": item.base_net_amount,
"credit_in_account_currency": item.base_net_amount \
@ -911,6 +911,91 @@ class SalesInvoice(SellingController):
for entry in self.payments:
if entry.amount < 0:
frappe.throw(_("Row #{0} (Payment Table): Amount must be positive").format(entry.idx))
def book_income_for_deferred_revenue(self):
# book the income on the last day, but it will be trigger on the 1st of month at 12:00 AM
# start_date: 1st of the last month or the start date
# end_date: end_date or today-1
gl_entries = []
for item in self.get('items'):
last_gl_entry = False
import pdb
# pdb.set_trace()
booking_start_date = getdate(add_months(today(), -1))
booking_start_date = booked_start_date if booking_start_date>item.service_start_date else item.service_start_date
booking_end_date = getdate(add_days(today(), -1))
if booking_end_date>=item.service_end_date:
last_gl_entry = True
booking_end_date = item.service_end_date
total_days = date_diff(item.service_end_date, item.service_start_date)
total_booking_days = date_diff(booking_end_date, booking_start_date)
account_currency = get_account_currency(item.income_account)
if not last_gl_entry:
base_amount = flt(item.base_net_amount * total_booking_days / flt(total_days))
if account_currency==self.company_currency:
amount = base_amount
else:
amount = flt(item.net_amount * total_booking_days / flt(total_days))
else:
base_amount = ''
amount = ''
gl_entries_details = frappe.db.sql('''
select sum(credit)-sum(debit) as balance, sum(credit_in_account_currency)-sum(debit_in_account_currency) as
balance_in_account_currency, voucher_no from `tabGL Entry`
where company=%s and account=%s and voucher_type=%s and voucher_no=%s group by voucher_no
''', (self.company, item.deferred_revenue_account, "Sales Invoice", self.name), as_dict=True)
# GL Entry for crediting the amount in the income
gl_entries.append(
self.get_gl_dict({
"account": item.income_account,
"against": self.customer,
"credit": base_amount,
"credit_in_account_currency": amount,
"cost_center": item.cost_center
}, account_currency)
)
# GL Entry to debit the amount from the deferred account
gl_entries.append(
self.get_gl_dict({
"account": item.deferred_revenue_account,
"against": self.customer,
"debit": base_amount,
"debit_in_account_currency": amount,
"cost_center": item.cost_center
}, account_currency)
)
# from erpnext.accounts.general_ledger import merge_similar_entries
# gl_entries = merge_similar_entries(gl_entries)
if gl_entries:
from erpnext.accounts.general_ledger import make_gl_entries
make_gl_entries(gl_entries, cancel=(self.docstatus == 2), merge_entries=True)
frappe.throw("stop")
def make_gl_entries_for_deferred_revenue(self, items, gl_entries):
pass
def booked_deferred_revenue():
# check for the sales invoice for which GL entries has to be done
print ("======================")
invoices = frappe.db.sql_list('''
select parent from `tabSales Invoice Item` where service_start_date<=%s and service_end_date>=%s
''', (today(), add_months(today(), -1)))
print (invoices)
# ToDo also find the list on the basic of the GL entry
for invoice in invoices:
doc = frappe.get_doc("Sales Invoice", invoice)
doc.book_income_for_deferred_revenue()
def get_list_context(context=None):
from erpnext.controllers.website_list_for_contact import get_list_context

View File

@ -235,6 +235,9 @@ scheduler_events = {
"erpnext.setup.doctype.company.company.cache_companies_monthly_sales_history",
"erpnext.manufacturing.doctype.bom_update_tool.bom_update_tool.update_latest_price_in_all_boms",
"erpnext.assets.doctype.asset.asset.update_maintenance_status"
],
"monthly": [
"erpnext.accounts.doctype.sales_invoice.sales_invoice.booked_deferred_revenue"
]
}

File diff suppressed because it is too large Load Diff

View File

@ -2650,19 +2650,18 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "deferred_revenue_account",
"fieldtype": "Link",
"fieldname": "enable_deferred_revenue",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 1,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Deferred Revenue Account",
"label": "Enable Deferred Revenue",
"length": 0,
"no_copy": 0,
"options": "Account",
"permlevel": 0,
"precision": "",
"print_hide": 0,
@ -2682,18 +2681,20 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "enable_deferred_revenue",
"fieldtype": "Check",
"depends_on": "enable_deferred_revenue",
"fieldname": "deferred_revenue_account",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_user_permissions": 1,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Enable Deferred Revenue",
"label": "Deferred Revenue Account",
"length": 0,
"no_copy": 0,
"options": "Account",
"permlevel": 0,
"precision": "",
"print_hide": 0,
@ -2743,8 +2744,9 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"depends_on": "enable_deferred_revenue",
"fieldname": "no_of_months",
"fieldtype": "Data",
"fieldtype": "Int",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
@ -3872,7 +3874,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 1,
"modified": "2018-05-14 02:25:13.690120",
"modified": "2018-05-14 05:03:30.856251",
"modified_by": "Administrator",
"module": "Stock",
"name": "Item",

View File

@ -4,7 +4,7 @@
from __future__ import unicode_literals
import frappe
from frappe import _, throw
from frappe.utils import flt, cint, add_days, cstr
from frappe.utils import flt, cint, add_days, cstr, add_months
import json
from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item, set_transaction_type
from erpnext.setup.utils import get_exchange_rate
@ -254,6 +254,15 @@ def get_basic_details(args, item):
"last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0
})
if item.enable_deferred_revenue:
service_end_date = add_months(args.transaction_date, item.no_of_months)
out.update({
"enable_deferred_revenue": item.enable_deferred_revenue,
"deferred_revenue_account": get_default_deferred_revenue_account(args, item),
"service_start_date": args.transaction_date,
"service_end_date": service_end_date
})
# calculate conversion factor
if item.stock_uom == args.uom:
out.conversion_factor = 1.0
@ -294,6 +303,14 @@ def get_default_expense_account(args, item):
or args.expense_account
or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
def get_default_deferred_revenue_account(args, item):
if item.enable_deferred_revenue:
return (item.deferred_revenue_account
or args.deferred_revenue_account
or frappe.db.get_value("Company", args.company, "default_deferred_revenue_account"))
else:
return None
def get_default_cost_center(args, item):
return (frappe.db.get_value("Project", args.get("project"), "cost_center")
or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)