<!DOCTYPE html>
<html lang="en">
{{fscript
	
	contacts := objectContext objectsForEntityNamed:'Contact'.
	all := {}.

	
	contacts do:[:contact |
	
		includeContact := contact birthdayOrAnniversaryFallsWithin:(NSDate date) :((NSDate date) mcDateByAddingDays:30).
		(includeContact ~= nil)ifTrue:
		[
			displayDate := nil.

			bDay := contact birthDay.
			bMonh := contact birthMonth.
			bYear := contact birthYear.
			date := NSDate mcDateWithYear:((NSDate date) mcYear) month:bMonh day:bDay.
			today := NSDate date mcDateAsBeginningOfDay.
			futureDate := ((NSDate date) mcDateByAddingDays:30) mcDateAsEndOfDay.
			((today <= date) & (date <= futureDate))ifTrue:
			[
				displayDate := date mcShortDate.
				displayDate := displayDate ++ '  Birthday'.
				
				dict := #{'contactRecord' -> (contact), 'month' -> (bMonh), 'day' -> (bDay), 'year' -> (bYear), 'eventType' -> ('Birthday')}.

				all add:dict.
				
			].
				
			"Check if the anniversary falls between these dates"
			anniDay := contact anniversaryDay.
			anniMonh := contact anniversaryMonth.
			anniYear := contact anniversaryYear.
			anniDate := NSDate mcDateWithYear:((NSDate date) mcYear) month:anniMonh day:anniDay.

			today := NSDate date mcDateAsBeginningOfDay.
			futureDate := ((NSDate date) mcDateByAddingDays:30) mcDateAsEndOfDay.
			((today <= anniDate) & (anniDate <= futureDate))ifTrue:
			[
				displayAnniDate := anniDate mcShortDate.
				displayAnniDate := displayAnniDate ++ '  Anniversary'.
				
				dict := #{'contactRecord' -> (contact), 'month' -> (anniMonh), 'day' -> (anniDay), 'year' -> (anniYear), 'eventType' -> ('Anniversary')}.
				all add:dict.
				
			].	
		].
	].
	
	sortedall := all sortedArrayUsingDescriptors:{(NSSortDescriptor sortDescriptorWithKey:'month' ascending:false), (NSSortDescriptor sortDescriptorWithKey:'day' ascending:true)}.
	globals setContactsWithEvent:sortedall.
}}

<head>
	<meta charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/default.css" type="text/css" charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/screen.css" type="text/css" charset="utf-8" media="screen">
	<link rel="stylesheet" href="{{framework}}/print.css" type="text/css" charset="utf-8" media="print">
	
</head>


<body>
	<h2>{{localize 'Birthdays and Anniversaries in the next 30 Days'}}</h2>
	<table>
		<thead>
				<td>{{localize 'First Name'}}</td>
				<td>{{localize 'Last name'}}</td>
				<td>{{localize 'Date'}}</td>
				<td>{{localize 'Event Type'}}</td>
		</thead>
		<tbody>{{foreach contact contactsWithEvent do}}
			<tr>
			<td>
				{{contact.contactRecord.firstname}}</a>
			</td>
			<td>
				{{contact.contactRecord.lastname}}
			</td>
			<td>
				{{fscript
					eventDate := nil.
					year := NSNumber numberWithInt:(contact valueForKey:'year').
					month := NSNumber numberWithInt:(contact valueForKey:'month').
					day :=  NSNumber numberWithInt:(contact valueForKey:'day').
					((contact valueForKey:'year') > 0)ifTrue:
					[
						aDate := NSDate mcDateWithYear:year month:month day:day.
						eventDate := aDate mcShortDate.
					].
					((contact valueForKey:'year') > 0)ifFalse:
					[
						eventDate := (month stringValue) ++ '-' ++ (day stringValue).
					].
					eventDate.

				}}
			</td>
			<td>
				{{contact.eventType}}
			</td>
			</tr>{{endforeach do}}
		</tbody>	
	</table>

	<br>
	<br>
	
	<br>
	<br>
	<div id="reportfooter">
	{{localize 'Generated by %@ on %@', objectContext.user.contact.cachedName, date}}.		
	</div>

</body>
</html>