<commit_before># Please edit this list and import only required elements
import webnotes

from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
from webnotes.model import db_exists
from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
from webnotes.model.doclist import getlist, copy_doclist
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
from webnotes import session, form, is_testing, msgprint, errprint

set = webnotes.conn.set
sql = webnotes.conn.sql
get_value = webnotes.conn.get_value
in_transaction = webnotes.conn.in_transaction
convert_to_lists = webnotes.conn.convert_to_lists
	
# -----------------------------------------------------------------------------------------

from utilities.transaction_base import TransactionBase

class DocType(TransactionBase):
	def __init__(self,d,dl):
		self.doc, self.doclist = d, dl
		self.log = []
		self.tname = 'RV Detail'
		self.fname = 'entries'


	# Autoname
	# ---------
	def autoname(self):
		self.doc.name = make_autoname(self.doc.naming_series+ '.#####')



# ********************************* Trigger Functions ******************************

	#Set retail related fields from pos settings
	#-------------------------------------------------------------------------
	def set_pos_fields(self):
		pos = sql("select * from `tabPOS Setting` where ifnull(user,'') = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)
		if not pos:
			pos = sql("select * from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
		if pos:
			val = sql("select name from `tabAccount` where name = %s and docstatus != 2", (cstr(self.doc.customer) + " - " + self.get_company_abbr()))
			val = val and val[0][0] or ''
			if not val: val = pos and pos[0]['customer_account'] or ''
			if not self.doc.debit_to:
				set(self.doc,'debit_to',val)
			
			lst = ['territory','naming_series','currency','charge','letter_head','tc_name','price_list_name','company','select_print_heading','cash_bank_account']
				
			for i in lst:
				val = pos and pos[0][i] or ''
				set(self.doc,i,val)
			self.set_pos_item_values()
			
			val = pos and flt(pos[0]['conversion_rate']) or 0	
			set(self.doc,'conversion_rate',val)

			#fetch terms	
			if self.doc.tc_name:	 self.get_tc_details()
			
			#fetch charges
			if self.doc.charge:		self.get_other_charges()


	# Set default values related to pos for previously created sales invoice.
	# -------------------------------------------------------------------------- 
	def set_pos_item_values(self):
		if cint(self.doc.is_pos) ==1:
			dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)
			if not dtl:
				dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
			for d in getlist(self.doclist,'entries'):
				if dtl and dtl[0]['income_account']: d.income_account = dtl[0]['income_account']
				if dtl and dtl[0]['cost_center']: d.cost_center = dtl[0]['cost_center']
				if dtl and dtl[0]['warehouse']: d.warehouse = dtl[0]['warehouse']

			
	# Get Account Head to which amount needs to be Debited based on Customer
	# ----------------------------------------------------------------------
	def get_customer_account(self):
		acc_head = sql("select name from `tabAccount` where (name = %s or (master_name = %s and master_type = 'customer')) and docstatus != 2", (cstr(self.doc.customer) + " - " + self.get_company_abbr(),self.doc.customer))
		if acc_head and acc_head[0][0]:
			return acc_head[0][0]
		else:
			msgprint("%s does not have an Account Head in %s. You must first create it from the Customer Master" % (self.doc.customer, self.doc.company))

	def get_debit_to(self):
		acc_head = self.get_customer_account()
		if acc_head:
			return { 'debit_to' : acc_head }


	# Set Due Date = Posting Date + Credit Days
	# -----------------------------------------
	def get_cust_and_due_date(self):
		credit_days = 0
		if self.doc.debit_to:
			credit_days = sql("select credit_days from `tabAccount` where name='%s' and docstatus != 2" % self.doc.debit_to)
			credit_days = credit_days and cint(credit_days[0][0]) or 0
		if self.doc.company and not credit_days:
			credit_days = sql("select credit_days from `tabCompany` where name='%s'" % self.doc.company)
			credit_days = credit_days and cint(credit_days[0][0]) or 0
		# Customer has higher priority than company
		# i.e.if not entered in customer will take credit days from company
		self.doc.due_date = add_days(cstr(self.doc.posting_date), credit_days)
		
		if self.doc.debit_to:
			self.doc.customer = get_value('Account',self.doc.debit_to,'master_name')
		#	get_obj('Sales Common').get_customer_details(self, inv_det_reqd = 0)


	# Pull Details of Delivery Note or Sales Order Selected
	# ------------------------------------------------------
	def pull_details(self):
		# Delivery Note
		if self.doc.delivery_note_main:
			self.validate_prev_docname('delivery note')
			self.doc.clear_table(self.doclist,'other_charges')			
			self.doclist = get_obj('DocType Mapper', 'Delivery Note-Receivable Voucher').dt_map('Delivery Note', 'Receivable Voucher', self.doc.delivery_note_main, self.doc, self.doclist, "[['Delivery Note', 'Receivable Voucher'],['Delivery Note Detail', 'RV Detail'],['RV Tax Detail','RV Tax Detail'],['Sales Team','Sales Team']]")			
			self.get_income_account('entries')
		# Sales Order
		elif self.doc.sales_order_main:
			self.validate_prev_docname('sales order')
			self.doc.clear_table(self.doclist,'other_charges')
			get_obj('DocType Mapper', 'Sales Order-Receivable Voucher').dt_map('Sales Order', 'Receivable Voucher', self.doc.sales_order_main, self.doc, self.doclist, "[['Sales Order', 'Receivable Voucher'],['Sales Order Detail', 'RV Detail'],['RV Tax Detail','RV Tax Detail'], ['Sales Team', 'Sales Team']]")
			self.get_income_account('entries')
			
		ret = eval(self.get_debit_to())	
		if ret.has_key('debit_to'):
			self.doc.debit_to = ret['debit_to']
					
	# onload pull income account
	# --------------------------
	def load_default_accounts(self):
		"""
			Loads default accounts from items, customer when called from mapper
		"""
		self.get_income_account('entries')
		self.doc.debit_to = self.get_customer_account()
		
	def get_income_account(self,doctype):		
		for d in getlist(self.doclist, doctype):			
			if d.item_code:
				item = sql("select default_income_account, default_sales_cost_center from tabItem where name = '%s'" %(d.item_code), as_dict=1)
				d.income_account = item and item[0]['default_income_account'] or ''
				d.cost_center = item and item[0]['default_sales_cost_center'] or ''				

	# Item Details
	# -------------
	def get_item_details(self, item_code):
		ret = get_obj('Sales Common').get_item_details(item_code, self)
		if item_code and cint(self.doc.is_pos) == 1:
			dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where user = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)				 
			if not dtl:
				dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
			if dtl and dtl[0]['income_account']: ret['income_account'] = dtl and dtl[0]['income_account']
			if dtl and dtl[0]['cost_center']: ret['cost_center'] = dtl and dtl[0]['cost_center']
			if dtl and dtl[0]['warehouse']: ret['warehouse'] = dtl and dtl[0]['warehouse']
			if ret['warehouse']:
				actual_qty = sql("select actual_qty from `tabBin` where item_code = '%s' and warehouse = '%s'" % (item_code, ret['warehouse']))		
				ret['actual_qty']= actual_qty and flt(actual_qty[0][0]) or 0
		return str(ret)
 

	# Get tax rate if account type is tax
	# ------------------------------------
	def get_rate(self,arg):
		get_obj('Sales Common').get_rate(arg)
		
		
	# Get Commission rate of Sales Partner
	# -------------------------------------
	def get_comm_rate(self, sales_partner):
		return get_obj('Sales Common').get_comm_rate(sales_partner, self)	
	
 <commit_msg>str removed for get_server_fields return<commit_after># Please edit this list and import only required elements
import webnotes

from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
from webnotes.model import db_exists
from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
from webnotes.model.doclist import getlist, copy_doclist
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
from webnotes import session, form, is_testing, msgprint, errprint

set = webnotes.conn.set
sql = webnotes.conn.sql
get_value = webnotes.conn.get_value
in_transaction = webnotes.conn.in_transaction
convert_to_lists = webnotes.conn.convert_to_lists
	
# -----------------------------------------------------------------------------------------

from utilities.transaction_base import TransactionBase

class DocType(TransactionBase):
	def __init__(self,d,dl):
		self.doc, self.doclist = d, dl
		self.log = []
		self.tname = 'RV Detail'
		self.fname = 'entries'


	# Autoname
	# ---------
	def autoname(self):
		self.doc.name = make_autoname(self.doc.naming_series+ '.#####')



# ********************************* Trigger Functions ******************************

	#Set retail related fields from pos settings
	#-------------------------------------------------------------------------
	def set_pos_fields(self):
		pos = sql("select * from `tabPOS Setting` where ifnull(user,'') = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)
		if not pos:
			pos = sql("select * from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
		if pos:
			val = sql("select name from `tabAccount` where name = %s and docstatus != 2", (cstr(self.doc.customer) + " - " + self.get_company_abbr()))
			val = val and val[0][0] or ''
			if not val: val = pos and pos[0]['customer_account'] or ''
			if not self.doc.debit_to:
				set(self.doc,'debit_to',val)
			
			lst = ['territory','naming_series','currency','charge','letter_head','tc_name','price_list_name','company','select_print_heading','cash_bank_account']
				
			for i in lst:
				val = pos and pos[0][i] or ''
				set(self.doc,i,val)
			self.set_pos_item_values()
			
			val = pos and flt(pos[0]['conversion_rate']) or 0	
			set(self.doc,'conversion_rate',val)

			#fetch terms	
			if self.doc.tc_name:	 self.get_tc_details()
			
			#fetch charges
			if self.doc.charge:		self.get_other_charges()


	# Set default values related to pos for previously created sales invoice.
	# -------------------------------------------------------------------------- 
	def set_pos_item_values(self):
		if cint(self.doc.is_pos) ==1:
			dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)
			if not dtl:
				dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
			for d in getlist(self.doclist,'entries'):
				if dtl and dtl[0]['income_account']: d.income_account = dtl[0]['income_account']
				if dtl and dtl[0]['cost_center']: d.cost_center = dtl[0]['cost_center']
				if dtl and dtl[0]['warehouse']: d.warehouse = dtl[0]['warehouse']

			
	# Get Account Head to which amount needs to be Debited based on Customer
	# ----------------------------------------------------------------------
	def get_customer_account(self):
		acc_head = sql("select name from `tabAccount` where (name = %s or (master_name = %s and master_type = 'customer')) and docstatus != 2", (cstr(self.doc.customer) + " - " + self.get_company_abbr(),self.doc.customer))
		if acc_head and acc_head[0][0]:
			return acc_head[0][0]
		else:
			msgprint("%s does not have an Account Head in %s. You must first create it from the Customer Master" % (self.doc.customer, self.doc.company))

	def get_debit_to(self):
		acc_head = self.get_customer_account()
		if acc_head:
			return { 'debit_to' : acc_head }


	# Set Due Date = Posting Date + Credit Days
	# -----------------------------------------
	def get_cust_and_due_date(self):
		credit_days = 0
		if self.doc.debit_to:
			credit_days = sql("select credit_days from `tabAccount` where name='%s' and docstatus != 2" % self.doc.debit_to)
			credit_days = credit_days and cint(credit_days[0][0]) or 0
		if self.doc.company and not credit_days:
			credit_days = sql("select credit_days from `tabCompany` where name='%s'" % self.doc.company)
			credit_days = credit_days and cint(credit_days[0][0]) or 0
		# Customer has higher priority than company
		# i.e.if not entered in customer will take credit days from company
		self.doc.due_date = add_days(cstr(self.doc.posting_date), credit_days)
		
		if self.doc.debit_to:
			self.doc.customer = get_value('Account',self.doc.debit_to,'master_name')
		#	get_obj('Sales Common').get_customer_details(self, inv_det_reqd = 0)


	# Pull Details of Delivery Note or Sales Order Selected
	# ------------------------------------------------------
	def pull_details(self):
		# Delivery Note
		if self.doc.delivery_note_main:
			self.validate_prev_docname('delivery note')
			self.doc.clear_table(self.doclist,'other_charges')			
			self.doclist = get_obj('DocType Mapper', 'Delivery Note-Receivable Voucher').dt_map('Delivery Note', 'Receivable Voucher', self.doc.delivery_note_main, self.doc, self.doclist, "[['Delivery Note', 'Receivable Voucher'],['Delivery Note Detail', 'RV Detail'],['RV Tax Detail','RV Tax Detail'],['Sales Team','Sales Team']]")			
			self.get_income_account('entries')
		# Sales Order
		elif self.doc.sales_order_main:
			self.validate_prev_docname('sales order')
			self.doc.clear_table(self.doclist,'other_charges')
			get_obj('DocType Mapper', 'Sales Order-Receivable Voucher').dt_map('Sales Order', 'Receivable Voucher', self.doc.sales_order_main, self.doc, self.doclist, "[['Sales Order', 'Receivable Voucher'],['Sales Order Detail', 'RV Detail'],['RV Tax Detail','RV Tax Detail'], ['Sales Team', 'Sales Team']]")
			self.get_income_account('entries')
			
		ret = eval(self.get_debit_to())	
		if ret.has_key('debit_to'):
			self.doc.debit_to = ret['debit_to']
					
	# onload pull income account
	# --------------------------
	def load_default_accounts(self):
		"""
			Loads default accounts from items, customer when called from mapper
		"""
		self.get_income_account('entries')
		self.doc.debit_to = self.get_customer_account()
		
	def get_income_account(self,doctype):		
		for d in getlist(self.doclist, doctype):			
			if d.item_code:
				item = sql("select default_income_account, default_sales_cost_center from tabItem where name = '%s'" %(d.item_code), as_dict=1)
				d.income_account = item and item[0]['default_income_account'] or ''
				d.cost_center = item and item[0]['default_sales_cost_center'] or ''				

	# Item Details
	# -------------
	def get_item_details(self, item_code):
		ret = get_obj('Sales Common').get_item_details(item_code, self)
		if item_code and cint(self.doc.is_pos) == 1:
			dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where user = '%s' and company = '%s'" % (session['user'], self.doc.company), as_dict=1)				 
			if not dtl:
				dtl = sql("select income_account, warehouse, cost_center from `tabPOS Setting` where ifnull(user,'') = '' and company = '%s'" % (self.doc.company), as_dict=1)
			if dtl and dtl[0]['income_account']: ret['income_account'] = dtl and dtl[0]['income_account']
			if dtl and dtl[0]['cost_center']: ret['cost_center'] = dtl and dtl[0]['cost_center']
			if dtl and dtl[0]['warehouse']: ret['warehouse'] = dtl and dtl[0]['warehouse']
			if ret['warehouse']:
				actual_qty = sql("select actual_qty from `tabBin` where item_code = '%s' and warehouse = '%s'" % (item_code, ret['warehouse']))		
				ret['actual_qty']= actual_qty and flt(actual_qty[0][0]) or 0
		return ret
 

	# Get tax rate if account type is tax
	# ------------------------------------
	def get_rate(self,arg):
		get_obj('Sales Common').get_rate(arg)
		
		
	# Get Commission rate of Sales Partner
	# -------------------------------------
	def get_comm_rate(self, sales_partner):
		return get_obj('Sales Common').get_comm_rate(sales_partner, self)	
	
 
{'fraction_non_alphanumeric': 0.19446067547037513, 'fraction_numerical': 0.0054925791749444895, 'mean_word_length': 3.536178107606679, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6740543', 'n_tokens_mistral': 6115, 'n_tokens_neox': 5508, 'n_words': 1376}