<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe import _, throw
from frappe.utils import cint, today, flt
from erpnext.setup.utils import get_company_currency, get_exchange_rate
from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year
from erpnext.utilities.transaction_base import TransactionBase
from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document
import json

class AccountsController(TransactionBase):
	def validate(self):
		if self.get("_action") and self._action != "update_after_submit":
			self.set_missing_values(for_validate=True)
		self.validate_date_with_fiscal_year()
		if self.meta.get_field("currency"):
			self.calculate_taxes_and_totals()
			self.validate_value("grand_total", ">=", 0)
			self.set_total_in_words()

		self.validate_for_freezed_account()

		if self.meta.get_field("is_recurring"):
			validate_recurring_document(self)

	def on_submit(self):
		if self.meta.get_field("is_recurring"):
			convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date"))

	def on_update_after_submit(self):
		if self.meta.get_field("is_recurring"):
			validate_recurring_document(self)
			convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date"))

	def before_recurring(self):
		self.fiscal_year = None
		for fieldname in ("due_date", "aging_date"):
			if self.meta.get_field(fieldname):
				self.set(fieldname, None)

	def set_missing_values(self, for_validate=False):
		for fieldname in ["posting_date", "transaction_date"]:
			if not self.get(fieldname) and self.meta.get_field(fieldname):
				self.set(fieldname, today())
				if not self.fiscal_year:
					self.fiscal_year = get_fiscal_year(self.get(fieldname))[0]
				break

	def validate_date_with_fiscal_year(self):
		if self.meta.get_field("fiscal_year") :
			date_field = ""
			if self.meta.get_field("posting_date"):
				date_field = "posting_date"
			elif self.meta.get_field("transaction_date"):
				date_field = "transaction_date"

			if date_field and self.get(date_field):
				validate_fiscal_year(self.get(date_field), self.fiscal_year,
					label=self.meta.get_label(date_field))

	def validate_for_freezed_account(self):
		for fieldname in ["customer", "supplier"]:
			if self.meta.get_field(fieldname) and self.get(fieldname):
				accounts = frappe.db.get_values("Account",
					{"master_type": fieldname.title(), "master_name": self.get(fieldname),
					"company": self.company}, "name")
				if accounts:
					from erpnext.accounts.doctype.gl_entry.gl_entry import validate_frozen_account
					for account in accounts:
						validate_frozen_account(account[0])

	def set_price_list_currency(self, buying_or_selling):
		if self.meta.get_field("currency"):
			company_currency = get_company_currency(self.company)

			# price list part
			fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \
				else "buying_price_list"
			if self.meta.get_field(fieldname) and self.get(fieldname):
				self.price_list_currency = frappe.db.get_value("Price List",
					self.get(fieldname), "currency")

				if self.price_list_currency == company_currency:
					self.plc_conversion_rate = 1.0

				elif not self.plc_conversion_rate:
					self.plc_conversion_rate = get_exchange_rate(
						self.price_list_currency, company_currency)

			# currency
			if not self.currency:
				self.currency = self.price_list_currency
				self.conversion_rate = self.plc_conversion_rate
			elif self.currency == company_currency:
				self.conversion_rate = 1.0
			elif not self.conversion_rate:
				self.conversion_rate = get_exchange_rate(self.currency,
					company_currency)

	def set_missing_item_details(self):
		"""set missing item values"""
		from erpnext.stock.get_item_details import get_item_details
		if hasattr(self, "fname"):
			parent_dict = {}
			for fieldname in self.meta.get_valid_columns():
				parent_dict[fieldname] = self.get(fieldname)

			for item in self.get(self.fname):
				if item.get("item_code"):
					args = parent_dict.copy()
					args.update(item.as_dict())
					ret = get_item_details(args)

					for fieldname, value in ret.items():
						if item.meta.get_field(fieldname) and \
							item.get(fieldname) is None and value is not None:
								item.set(fieldname, value)

					if ret.get("pricing_rule"):<commit_msg>set missing cost center using default<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe import _, throw
from frappe.utils import cint, today, flt
from erpnext.setup.utils import get_company_currency, get_exchange_rate
from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year
from erpnext.utilities.transaction_base import TransactionBase
from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document
import json

class AccountsController(TransactionBase):
	def validate(self):
		if self.get("_action") and self._action != "update_after_submit":
			self.set_missing_values(for_validate=True)
		self.validate_date_with_fiscal_year()
		if self.meta.get_field("currency"):
			self.calculate_taxes_and_totals()
			self.validate_value("grand_total", ">=", 0)
			self.set_total_in_words()

		self.validate_for_freezed_account()

		if self.meta.get_field("is_recurring"):
			validate_recurring_document(self)

	def on_submit(self):
		if self.meta.get_field("is_recurring"):
			convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date"))

	def on_update_after_submit(self):
		if self.meta.get_field("is_recurring"):
			validate_recurring_document(self)
			convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date"))

	def before_recurring(self):
		self.fiscal_year = None
		for fieldname in ("due_date", "aging_date"):
			if self.meta.get_field(fieldname):
				self.set(fieldname, None)

	def set_missing_values(self, for_validate=False):
		for fieldname in ["posting_date", "transaction_date"]:
			if not self.get(fieldname) and self.meta.get_field(fieldname):
				self.set(fieldname, today())
				if not self.fiscal_year:
					self.fiscal_year = get_fiscal_year(self.get(fieldname))[0]
				break

	def validate_date_with_fiscal_year(self):
		if self.meta.get_field("fiscal_year") :
			date_field = ""
			if self.meta.get_field("posting_date"):
				date_field = "posting_date"
			elif self.meta.get_field("transaction_date"):
				date_field = "transaction_date"

			if date_field and self.get(date_field):
				validate_fiscal_year(self.get(date_field), self.fiscal_year,
					label=self.meta.get_label(date_field))

	def validate_for_freezed_account(self):
		for fieldname in ["customer", "supplier"]:
			if self.meta.get_field(fieldname) and self.get(fieldname):
				accounts = frappe.db.get_values("Account",
					{"master_type": fieldname.title(), "master_name": self.get(fieldname),
					"company": self.company}, "name")
				if accounts:
					from erpnext.accounts.doctype.gl_entry.gl_entry import validate_frozen_account
					for account in accounts:
						validate_frozen_account(account[0])

	def set_price_list_currency(self, buying_or_selling):
		if self.meta.get_field("currency"):
			company_currency = get_company_currency(self.company)

			# price list part
			fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \
				else "buying_price_list"
			if self.meta.get_field(fieldname) and self.get(fieldname):
				self.price_list_currency = frappe.db.get_value("Price List",
					self.get(fieldname), "currency")

				if self.price_list_currency == company_currency:
					self.plc_conversion_rate = 1.0

				elif not self.plc_conversion_rate:
					self.plc_conversion_rate = get_exchange_rate(
						self.price_list_currency, company_currency)

			# currency
			if not self.currency:
				self.currency = self.price_list_currency
				self.conversion_rate = self.plc_conversion_rate
			elif self.currency == company_currency:
				self.conversion_rate = 1.0
			elif not self.conversion_rate:
				self.conversion_rate = get_exchange_rate(self.currency,
					company_currency)

	def set_missing_item_details(self):
		"""set missing item values"""
		from erpnext.stock.get_item_details import get_item_details
		if hasattr(self, "fname"):
			parent_dict = {}
			for fieldname in self.meta.get_valid_columns():
				parent_dict[fieldname] = self.get(fieldname)

			for item in self.get(self.fname):
				if item.get("item_code"):
					args = parent_dict.copy()
					args.update(item.as_dict())
					ret = get_item_details(args)

					for fieldname, value in ret.items():
						if item.meta.get_field(fieldname) and \
							item.get(fieldname) is None and value is not None:
								item.set(fieldname, value)

						if fieldname == "cost_center" and item.meta.get_field("cost_center") \
							and not item.get("cost_center") and value is not None:
								item.set(fieldname, value)

					if ret.get("pricing_rule"):
{'fraction_non_alphanumeric': 0.10577236656158279, 'fraction_numerical': 0.0026089792368735733, 'mean_word_length': 4.8898847631242, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2546529', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3070, 'n_words': 593}