# Copyright (c) 2015, SolutionERP LLC and Contributors
# License: GNU General Public License v3. See license.txt

import unittest

import xhiveframework

test_records = xhiveframework.get_test_records("Cost Center")


class TestCostCenter(unittest.TestCase):
	def test_cost_center_creation_against_child_node(self):

		if not xhiveframework.db.get_value("Cost Center", {"name": "_Test Cost Center 2 - _TC"}):
			xhiveframework.get_doc(test_records[1]).insert()

		cost_center = xhiveframework.get_doc(
			{
				"doctype": "Cost Center",
				"cost_center_name": "_Test Cost Center 3",
				"parent_cost_center": "_Test Cost Center 2 - _TC",
				"is_group": 0,
				"company": "_Test Company",
			}
		)

		self.assertRaises(xhiveframework.ValidationError, cost_center.save)


def create_cost_center(**args):
	args = xhiveframework._dict(args)
	if args.cost_center_name:
		company = args.company or "_Test Company"
		company_abbr = xhiveframework.db.get_value("Company", company, "abbr")
		cc_name = args.cost_center_name + " - " + company_abbr
		if not xhiveframework.db.exists("Cost Center", cc_name):
			cc = xhiveframework.new_doc("Cost Center")
			cc.company = args.company or "_Test Company"
			cc.cost_center_name = args.cost_center_name
			cc.is_group = args.is_group or 0
			cc.parent_cost_center = args.parent_cost_center or "_Test Company - _TC"
			cc.insert()
