############################
## DMSContainer
############################

#_totals = {}
import json

def get_functions(jinja_globals):
	jinja_globals.update({"_totals":{}})	

	def add(value, context = "default"):		
		nonlocal jinja_globals
		_totals = jinja_globals["_totals"]
		if context in _totals:
			v = float(jinja_globals["_totals"][context])
			v = v + float(value)
		else:
			v = value		
		jinja_globals["_totals"][context] = v
		return value

	def subtotal(context = "default"):
		nonlocal jinja_globals
		if context in jinja_globals["_totals"]:
			res = jinja_globals["_totals"][context]
		else:
			res = 0
		return res

	return {
		"subtotadd": add,
		"subtotget": subtotal
	}

def get_filters(jinja_globals):	
	def now(par, format = None):
		import datetime
		if format is None:
			return str(datetime.datetime.now())
		else:
			return datetime.datetime.now().strftime(format)

	def cleartotal(value, context = "default"):
		nonlocal jinja_globals
		jinja_globals["_totals"][context] = 0
		return value

	def currency(value, format = "x"):
		return value

	# def setlocale(locale_code):
	# 	import locale
	# 	locale.setlocale(locale.LC_ALL, locale_code)

	return {
		"count": len,
		"now": now,
		"subtotclear": cleartotal
		}
