<commit_before># ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.	If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
import webnotes

from webnotes.utils import add_days, add_years, cint, getdate
from webnotes.model import db_exists
from webnotes.model.doc import addchild, make_autoname
from webnotes.model.wrapper import copy_doclist
from webnotes import msgprint

sql = webnotes.conn.sql

import datetime

class DocType:
	def __init__(self,doc,doclist=[]):
		self.doc = doc
		self.doclist = doclist

	def autoname(self):
		self.doc.name = make_autoname(self.doc.fiscal_year +"/"+ self.doc.holiday_list_name+"/.###")
		
	def validate(self):
		self.update_default_holiday_list()
	
	def get_weekly_off_dates(self):
		self.validate_values()
		yr_start_date, yr_end_date = self.get_fy_start_end_dates()
		date_list = self.get_weekly_off_date_list(yr_start_date, yr_end_date)
		for d in date_list:
			ch = addchild(self.doc, 'holiday_list_details', 'Holiday', self.doclist)
			ch.description = self.doc.weekly_off
			ch.holiday_date = d

	def validate_values(self):
		if not self.doc.fiscal_year:
			msgprint("Please select Fiscal Year")
			raise Exception
		if not self.doc.weekly_off:
			msgprint("Please select weekly off day")
			raise Exception

	def get_fy_start_end_dates(self):
		return webnotes.conn.sql("""select year_start_date, 
			subdate(adddate(year_start_date, interval 1 year), interval 1 day) 
				as year_end_date
			from `tabFiscal Year`
			where name=%s""", (self.doc.fiscal_year,))[0]

	def get_weekly_off_date_list(self, year_start_date, year_end_date):
		from webnotes.utils import getdate
		year_start_date, year_end_date = getdate(year_start_date), getdate(year_end_date)
		
		from dateutil import relativedelta
		from datetime import timedelta
		import calendar
		
		date_list = []
		weekday = getattr(calendar, (self.doc.weekly_off).upper())
		reference_date = year_start_date + relativedelta.relativedelta(weekday=weekday)
			
		while reference_date <= year_end_date:
			date_list.append(reference_date)
			reference_date += timedelta(days=7)<commit_msg>add to holiday list sorted by idx<commit_after># ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.	If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
import webnotes

from webnotes.utils import add_days, add_years, cint, getdate
from webnotes.model import db_exists
from webnotes.model.doc import addchild, make_autoname
from webnotes.model.wrapper import copy_doclist
from webnotes import msgprint

sql = webnotes.conn.sql

import datetime

class DocType:
	def __init__(self,doc,doclist=[]):
		self.doc = doc
		self.doclist = doclist

	def autoname(self):
		self.doc.name = make_autoname(self.doc.fiscal_year +"/"+ self.doc.holiday_list_name+"/.###")
		
	def validate(self):
		self.update_default_holiday_list()
	
	def get_weekly_off_dates(self):
		self.validate_values()
		yr_start_date, yr_end_date = self.get_fy_start_end_dates()
		date_list = self.get_weekly_off_date_list(yr_start_date, yr_end_date)
		last_idx = max([cint(d.idx) for d in self.doclist.get(
			{"parentfield": "holiday_list_details"})] or [0,])
		for i, d in enumerate(date_list):
			ch = addchild(self.doc, 'holiday_list_details', 'Holiday', self.doclist)
			ch.description = self.doc.weekly_off
			ch.holiday_date = d
			ch.idx = last_idx + i + 1

	def validate_values(self):
		if not self.doc.fiscal_year:
			msgprint("Please select Fiscal Year")
			raise Exception
		if not self.doc.weekly_off:
			msgprint("Please select weekly off day")
			raise Exception

	def get_fy_start_end_dates(self):
		return webnotes.conn.sql("""select year_start_date, 
			subdate(adddate(year_start_date, interval 1 year), interval 1 day) 
				as year_end_date
			from `tabFiscal Year`
			where name=%s""", (self.doc.fiscal_year,))[0]

	def get_weekly_off_date_list(self, year_start_date, year_end_date):
		from webnotes.utils import getdate
		year_start_date, year_end_date = getdate(year_start_date), getdate(year_end_date)
		
		from dateutil import relativedelta
		from datetime import timedelta
		import calendar
		
		date_list = []
		weekday = getattr(calendar, (self.doc.weekly_off).upper())
		reference_date = year_start_date + relativedelta.relativedelta(weekday=weekday)
			
		while reference_date <= year_end_date:
			date_list.append(reference_date)
			reference_date += timedelta(days=7)
{'fraction_non_alphanumeric': 0.08779028852920479, 'fraction_numerical': 0.003518648838845883, 'mean_word_length': 4.836755646817249, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7472855', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1845, 'n_words': 542}