1f0a11419SGreg Roach<?php 2f0a11419SGreg Roach/** 3f0a11419SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify 6f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by 7f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8f0a11419SGreg Roach * (at your option) any later version. 9f0a11419SGreg Roach * This program is distributed in the hope that it will be useful, 10f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12f0a11419SGreg Roach * GNU General Public License for more details. 13f0a11419SGreg Roach * You should have received a copy of the GNU General Public License 14f0a11419SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15f0a11419SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 18f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services; 19f0a11419SGreg Roach 20f0a11419SGreg Roachuse Fisharebest\Webtrees\Date; 2125d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate; 22f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate; 23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 28f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact; 29f0a11419SGreg Roachuse Fisharebest\Webtrees\Family; 30f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 31f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual; 32f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree; 339e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 349e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder; 359e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause; 36f0a11419SGreg Roach 37f0a11419SGreg Roach/** 38f0a11419SGreg Roach * Calculate anniversaries, etc. 39f0a11419SGreg Roach */ 40f0a11419SGreg Roachclass CalendarService 41f0a11419SGreg Roach{ 42f0a11419SGreg Roach /** 4370d8159dSGreg Roach * List all the months in a given year. 4470d8159dSGreg Roach * 4570d8159dSGreg Roach * @param string $calendar 4670d8159dSGreg Roach * @param int $year 4770d8159dSGreg Roach * 4870d8159dSGreg Roach * @return string[] 4970d8159dSGreg Roach */ 5070d8159dSGreg Roach public function calendarMonthsInYear(string $calendar, int $year): array 5170d8159dSGreg Roach { 5270d8159dSGreg Roach $date = new Date($calendar . ' ' . $year); 5370d8159dSGreg Roach $calendar_date = $date->minimumDate(); 5470d8159dSGreg Roach $month_numbers = range(1, $calendar_date->monthsInYear()); 5570d8159dSGreg Roach $month_names = []; 5670d8159dSGreg Roach 5770d8159dSGreg Roach foreach ($month_numbers as $month_number) { 584a83f5d7SGreg Roach $calendar_date->day = 1; 594a83f5d7SGreg Roach $calendar_date->month = $month_number; 6070d8159dSGreg Roach $calendar_date->setJdFromYmd(); 6170d8159dSGreg Roach 6270d8159dSGreg Roach if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 6370d8159dSGreg Roach // No month 6 in Jewish non-leap years. 6470d8159dSGreg Roach continue; 6570d8159dSGreg Roach } 6670d8159dSGreg Roach 6770d8159dSGreg Roach if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 6870d8159dSGreg Roach // Month 7 is ADR in Jewish non-leap years (and ADS in others). 6970d8159dSGreg Roach $mon = 'ADR'; 7070d8159dSGreg Roach } else { 7170d8159dSGreg Roach $mon = $calendar_date->format('%O'); 7270d8159dSGreg Roach } 7370d8159dSGreg Roach 7470d8159dSGreg Roach $month_names[$mon] = $calendar_date->format('%F'); 7570d8159dSGreg Roach } 7670d8159dSGreg Roach 7770d8159dSGreg Roach return $month_names; 7870d8159dSGreg Roach } 7970d8159dSGreg Roach 8070d8159dSGreg Roach /** 81f0a11419SGreg Roach * Get a list of events which occured during a given date range. 82f0a11419SGreg Roach * 83f0a11419SGreg Roach * @param int $jd1 the start range of julian day 84f0a11419SGreg Roach * @param int $jd2 the end range of julian day 859e87c1a2SGreg Roach * @param string[] $facts restrict the search to just these facts or leave blank for all 86f0a11419SGreg Roach * @param Tree $tree the tree to search 87f0a11419SGreg Roach * 88f0a11419SGreg Roach * @return Fact[] 89f0a11419SGreg Roach */ 909e87c1a2SGreg Roach public function getCalendarEvents(int $jd1, int $jd2, array $facts, Tree $tree): array 91f0a11419SGreg Roach { 92f0a11419SGreg Roach // If no facts specified, get all except these 939e87c1a2SGreg Roach $skipfacts = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR']; 94f0a11419SGreg Roach 95f0a11419SGreg Roach // Events that start or end during the period 9625d4b688SGreg Roach $query = DB::table('dates') 979e87c1a2SGreg Roach ->where('d_file', '=', $tree->id()) 989e87c1a2SGreg Roach ->where(function (Builder $query) use ($jd1, $jd2) { 999e87c1a2SGreg Roach $query->where(function (Builder $query) use ($jd1, $jd2) { 1009e87c1a2SGreg Roach $query 1019e87c1a2SGreg Roach ->where('d_julianday1', '>=', $jd1) 1029e87c1a2SGreg Roach ->where('d_julianday1', '<=', $jd2); 1039e87c1a2SGreg Roach })->orWhere(function (Builder $query) use ($jd1, $jd2) { 1049e87c1a2SGreg Roach $query 1059e87c1a2SGreg Roach ->where('d_julianday2', '>=', $jd1) 1069e87c1a2SGreg Roach ->where('d_julianday2', '<=', $jd2); 1079e87c1a2SGreg Roach }); 1089e87c1a2SGreg Roach }); 109f0a11419SGreg Roach 110f0a11419SGreg Roach // Restrict to certain types of fact 111f0a11419SGreg Roach if (empty($facts)) { 1129e87c1a2SGreg Roach $query->whereNotIn('d_fact', $skipfacts); 113f0a11419SGreg Roach } else { 1149e87c1a2SGreg Roach $query->whereIn('d_fact', $facts); 115f0a11419SGreg Roach } 116f0a11419SGreg Roach 11725d4b688SGreg Roach $ind_query = (clone $query) 11825d4b688SGreg Roach ->join('individuals', function (JoinClause $join): void { 11925d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 12025d4b688SGreg Roach }) 12125d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 122f0a11419SGreg Roach 12325d4b688SGreg Roach $fam_query = (clone $query) 12425d4b688SGreg Roach ->join('families', function (JoinClause $join): void { 12525d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 12625d4b688SGreg Roach }) 12725d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 12825d4b688SGreg Roach 12925d4b688SGreg Roach // Now fetch these events 13025d4b688SGreg Roach $found_facts = []; 13125d4b688SGreg Roach 13225d4b688SGreg Roach foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) { 13325d4b688SGreg Roach foreach ($query->get() as $row) { 134f0a11419SGreg Roach if ($type === 'INDI') { 135f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 136f0a11419SGreg Roach } else { 137f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 138f0a11419SGreg Roach } 13925d4b688SGreg Roach 140f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 14125d4b688SGreg Roach 14230158ae7SGreg Roach foreach ($record->facts() as $fact) { 143f0a11419SGreg Roach // For date ranges, we need a match on either the start/end. 14425d4b688SGreg Roach if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 145f0a11419SGreg Roach $fact->anniv = 0; 146f0a11419SGreg Roach $found_facts[] = $fact; 147f0a11419SGreg Roach } 148f0a11419SGreg Roach } 149f0a11419SGreg Roach } 150f0a11419SGreg Roach } 151f0a11419SGreg Roach 152f0a11419SGreg Roach return $found_facts; 153f0a11419SGreg Roach } 154f0a11419SGreg Roach 155f0a11419SGreg Roach /** 156f0a11419SGreg Roach * Get the list of current and upcoming events, sorted by anniversary date 157f0a11419SGreg Roach * 158f0a11419SGreg Roach * @param int $jd1 159f0a11419SGreg Roach * @param int $jd2 160f0a11419SGreg Roach * @param string $events 161c7ff4153SGreg Roach * @param bool $only_living 162f0a11419SGreg Roach * @param string $sort_by 163f0a11419SGreg Roach * @param Tree $tree 164f0a11419SGreg Roach * 165f0a11419SGreg Roach * @return Fact[] 166f0a11419SGreg Roach */ 167f0a11419SGreg Roach public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array 168f0a11419SGreg Roach { 169f0a11419SGreg Roach $found_facts = []; 170f0a11419SGreg Roach $facts = []; 171f0a11419SGreg Roach 172f0a11419SGreg Roach foreach (range($jd1, $jd2) as $jd) { 173f0a11419SGreg Roach $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 174f0a11419SGreg Roach } 175f0a11419SGreg Roach 176f0a11419SGreg Roach foreach ($found_facts as $fact) { 177e7766c08SGreg Roach $record = $fact->record(); 178f0a11419SGreg Roach // only living people ? 179f0a11419SGreg Roach if ($only_living) { 180f0a11419SGreg Roach if ($record instanceof Individual && $record->isDead()) { 181f0a11419SGreg Roach continue; 182f0a11419SGreg Roach } 183f0a11419SGreg Roach if ($record instanceof Family) { 18439ca88baSGreg Roach $husb = $record->husband(); 1858f038c36SRico Sonntag if ($husb === null || $husb->isDead()) { 186f0a11419SGreg Roach continue; 187f0a11419SGreg Roach } 18839ca88baSGreg Roach $wife = $record->wife(); 1898f038c36SRico Sonntag if ($wife === null || $wife->isDead()) { 190f0a11419SGreg Roach continue; 191f0a11419SGreg Roach } 192f0a11419SGreg Roach } 193f0a11419SGreg Roach } 194f0a11419SGreg Roach $facts[] = $fact; 195f0a11419SGreg Roach } 196f0a11419SGreg Roach 197f0a11419SGreg Roach switch ($sort_by) { 198f0a11419SGreg Roach case 'anniv': 199*1c7a6874SGreg Roach $facts = Fact::sortFacts($facts)->all(); 200f0a11419SGreg Roach break; 201580a4d11SGreg Roach 202f0a11419SGreg Roach case 'alpha': 203a6312006SGreg Roach uasort($facts, function (Fact $x, Fact $y): int { 204c156e8f5SGreg Roach return GedcomRecord::nameComparator()($x->record(), $y->record()); 205f0a11419SGreg Roach }); 206f0a11419SGreg Roach break; 207f0a11419SGreg Roach } 208f0a11419SGreg Roach 209f0a11419SGreg Roach return $facts; 210f0a11419SGreg Roach } 211f0a11419SGreg Roach 212f0a11419SGreg Roach /** 213f0a11419SGreg Roach * Get a list of events whose anniversary occured on a given julian day. 214f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 215f0a11419SGreg Roach * 216f0a11419SGreg Roach * @param int $jd the julian day 217f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 218f0a11419SGreg Roach * @param Tree $tree the tree to search 219f0a11419SGreg Roach * 220f0a11419SGreg Roach * @return Fact[] 221f0a11419SGreg Roach */ 22225d4b688SGreg Roach public function getAnniversaryEvents($jd, string $facts, Tree $tree): array 223f0a11419SGreg Roach { 224f0a11419SGreg Roach $found_facts = []; 225f0a11419SGreg Roach 226f0a11419SGreg Roach $anniversaries = [ 227f0a11419SGreg Roach new GregorianDate($jd), 228f0a11419SGreg Roach new JulianDate($jd), 229f0a11419SGreg Roach new FrenchDate($jd), 230f0a11419SGreg Roach new JewishDate($jd), 231f0a11419SGreg Roach new HijriDate($jd), 232f0a11419SGreg Roach new JalaliDate($jd), 233f0a11419SGreg Roach ]; 234f0a11419SGreg Roach 235f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 23625d4b688SGreg Roach // Build a query to match anniversaries in the appropriate calendar. 23725d4b688SGreg Roach $query = DB::table('dates') 23825d4b688SGreg Roach ->distinct() 23925d4b688SGreg Roach ->where('d_file', '=', $tree->id()) 24025d4b688SGreg Roach ->where('d_type', '=', $anniv->format('%@')); 241f0a11419SGreg Roach 242f0a11419SGreg Roach // SIMPLE CASES: 243f0a11419SGreg Roach // a) Non-hebrew anniversaries 244f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 24525d4b688SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13])) { 24625d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 247f0a11419SGreg Roach } else { 248f0a11419SGreg Roach // SPECIAL CASES: 2494a83f5d7SGreg Roach switch ($anniv->month) { 250f0a11419SGreg Roach case 2: 25125d4b688SGreg Roach $this->cheshvanAnniversaries($query, $anniv); 252f0a11419SGreg Roach break; 253f0a11419SGreg Roach case 3: 25425d4b688SGreg Roach $this->kislevAnniversaries($query, $anniv); 255f0a11419SGreg Roach break; 256f0a11419SGreg Roach case 4: 25725d4b688SGreg Roach $this->tevetAnniversaries($query, $anniv); 258f0a11419SGreg Roach break; 25925d4b688SGreg Roach case 7: 26025d4b688SGreg Roach $this->adarIIAnniversaries($query, $anniv); 261f0a11419SGreg Roach break; 26225d4b688SGreg Roach case 8: 26325d4b688SGreg Roach $this->nisanAnniversaries($query, $anniv); 264f0a11419SGreg Roach break; 265f0a11419SGreg Roach } 266f0a11419SGreg Roach } 267f0a11419SGreg Roach // Only events in the past (includes dates without a year) 26825d4b688SGreg Roach $query->where('d_year', '<=', $anniv->year()); 269f0a11419SGreg Roach 270f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 27125d4b688SGreg Roach 27225d4b688SGreg Roach if (!empty($matches[1])) { 27325d4b688SGreg Roach // Restrict to certain types of fact 27425d4b688SGreg Roach $query->whereIn('d_fact', $matches[1]); 275f0a11419SGreg Roach } else { 276f0a11419SGreg Roach // If no facts specified, get all except these 27725d4b688SGreg Roach $query->whereNotIn('d_fact', ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', '_TODO']); 278f0a11419SGreg Roach } 279f0a11419SGreg Roach 28025d4b688SGreg Roach $query 28125d4b688SGreg Roach ->orderby('d_day') 28225d4b688SGreg Roach ->orderbyDesc('d_year'); 28325d4b688SGreg Roach 28425d4b688SGreg Roach $ind_query = (clone $query) 28525d4b688SGreg Roach ->join('individuals', function (JoinClause $join): void { 28625d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 28725d4b688SGreg Roach }) 28825d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 28925d4b688SGreg Roach 29025d4b688SGreg Roach $fam_query = (clone $query) 29125d4b688SGreg Roach ->join('families', function (JoinClause $join): void { 29225d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 29325d4b688SGreg Roach }) 29425d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 295f0a11419SGreg Roach 296f0a11419SGreg Roach // Now fetch these anniversaries 29725d4b688SGreg Roach foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) { 29825d4b688SGreg Roach foreach ($query->get() as $row) { 299f0a11419SGreg Roach if ($type === 'INDI') { 300f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 301f0a11419SGreg Roach } else { 302f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 303f0a11419SGreg Roach } 30425d4b688SGreg Roach 305f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 30625d4b688SGreg Roach 30730158ae7SGreg Roach foreach ($record->facts() as $fact) { 3082decada7SGreg Roach if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 3094a83f5d7SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 310f0a11419SGreg Roach $fact->jd = $jd; 311f0a11419SGreg Roach $found_facts[] = $fact; 312f0a11419SGreg Roach } 313f0a11419SGreg Roach } 314f0a11419SGreg Roach } 315f0a11419SGreg Roach } 316f0a11419SGreg Roach } 317f0a11419SGreg Roach 318f0a11419SGreg Roach return $found_facts; 319f0a11419SGreg Roach } 32025d4b688SGreg Roach 32125d4b688SGreg Roach /** 32225d4b688SGreg Roach * By default, missing days have anniversaries on the first of the month, 32325d4b688SGreg Roach * and invalid days have anniversaries on the last day of the month. 32425d4b688SGreg Roach * 32525d4b688SGreg Roach * @param Builder $query 32625d4b688SGreg Roach * @param AbstractCalendarDate $anniv 32725d4b688SGreg Roach */ 32825d4b688SGreg Roach private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void 32925d4b688SGreg Roach { 33025d4b688SGreg Roach if ($anniv->day() === 1) { 33125d4b688SGreg Roach $query->where('d_day', '<=', 1); 33225d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 33325d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 33425d4b688SGreg Roach } else { 33525d4b688SGreg Roach $query->where('d_day', '=', $anniv->day()); 33625d4b688SGreg Roach } 33725d4b688SGreg Roach 33825d4b688SGreg Roach $query->where('d_mon', '=', $anniv->month()); 33925d4b688SGreg Roach } 34025d4b688SGreg Roach 34125d4b688SGreg Roach /** 34225d4b688SGreg Roach * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH). 34325d4b688SGreg Roach * 34425d4b688SGreg Roach * @param Builder $query 34525d4b688SGreg Roach * @param JewishDate $anniv 34625d4b688SGreg Roach */ 34725d4b688SGreg Roach private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void 34825d4b688SGreg Roach { 34925d4b688SGreg Roach if ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 35025d4b688SGreg Roach $query 35125d4b688SGreg Roach ->where('d_mon', '=', 2) 35225d4b688SGreg Roach ->where('d_day', '>=', 29) 35325d4b688SGreg Roach ->where('d_day', '<>', 30); 35425d4b688SGreg Roach } else { 35525d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 35625d4b688SGreg Roach } 35725d4b688SGreg Roach } 35825d4b688SGreg Roach 35925d4b688SGreg Roach /** 36025d4b688SGreg Roach * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH). 36125d4b688SGreg Roach * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL). 36225d4b688SGreg Roach * 36325d4b688SGreg Roach * @param Builder $query 36425d4b688SGreg Roach * @param JewishDate $anniv 36525d4b688SGreg Roach */ 36625d4b688SGreg Roach private function kislevAnniversaries(Builder $query, JewishDate $anniv): void 36725d4b688SGreg Roach { 36825d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']); 36925d4b688SGreg Roach 37025d4b688SGreg Roach if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) { 37125d4b688SGreg Roach $query->where(function (Builder $query): void { 37225d4b688SGreg Roach $query->where(function (Builder $query): void { 37325d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 3); 37425d4b688SGreg Roach })->orWhere(function (Builder $query): void { 37525d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 2); 37625d4b688SGreg Roach }); 37725d4b688SGreg Roach }); 37825d4b688SGreg Roach } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 37925d4b688SGreg Roach $query 38025d4b688SGreg Roach ->where('d_mon', '=', 3) 38125d4b688SGreg Roach ->where('d_day', '>=', 29) 38225d4b688SGreg Roach ->where('d_day', '<>', 30); 38325d4b688SGreg Roach } else { 38425d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 38525d4b688SGreg Roach } 38625d4b688SGreg Roach } 38725d4b688SGreg Roach 38825d4b688SGreg Roach /** 38925d4b688SGreg Roach * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL). 39025d4b688SGreg Roach * 39125d4b688SGreg Roach * @param Builder $query 39225d4b688SGreg Roach * @param JewishDate $anniv 39325d4b688SGreg Roach */ 39425d4b688SGreg Roach private function tevetAnniversaries(Builder $query, JewishDate $anniv): void 39525d4b688SGreg Roach { 39625d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']); 39725d4b688SGreg Roach 39825d4b688SGreg Roach if ($anniv->day === 1 && $tmp->daysInMonth() === 29) { 39925d4b688SGreg Roach $query->where(function (Builder $query): void { 40025d4b688SGreg Roach $query->where(function (Builder $query): void { 40125d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 4); 40225d4b688SGreg Roach })->orWhere(function (Builder $query): void { 40325d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 3); 40425d4b688SGreg Roach }); 40525d4b688SGreg Roach }); 40625d4b688SGreg Roach } else { 40725d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 40825d4b688SGreg Roach } 40925d4b688SGreg Roach } 41025d4b688SGreg Roach 41125d4b688SGreg Roach /** 41225d4b688SGreg Roach * ADS includes non-leap ADR. 41325d4b688SGreg Roach * 41425d4b688SGreg Roach * @param Builder $query 41525d4b688SGreg Roach * @param JewishDate $anniv 41625d4b688SGreg Roach */ 41725d4b688SGreg Roach private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void 41825d4b688SGreg Roach { 41925d4b688SGreg Roach if ($anniv->day() === 1) { 42025d4b688SGreg Roach $query->where('d_day', '<=', 1); 42125d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 42225d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 42325d4b688SGreg Roach } else { 42425d4b688SGreg Roach $query->where('d_day', '<=', 1); 42525d4b688SGreg Roach } 42625d4b688SGreg Roach 42725d4b688SGreg Roach $query->where(function (Builder $query): void { 42825d4b688SGreg Roach $query 42925d4b688SGreg Roach ->where('d_mon', '=', 7) 43025d4b688SGreg Roach ->orWhere(function (Builder $query): void { 43125d4b688SGreg Roach $query 43225d4b688SGreg Roach ->where('d_mon', '=', 6) 43325d4b688SGreg Roach ->where(DB::raw('MOD(7 * d_year + 1, 19)'), '>=', 7); 43425d4b688SGreg Roach }); 43525d4b688SGreg Roach }); 43625d4b688SGreg Roach } 43725d4b688SGreg Roach 43825d4b688SGreg Roach /** 43925d4b688SGreg Roach * 1 NSN includes 30 ADR, if this year is non-leap. 44025d4b688SGreg Roach * 44125d4b688SGreg Roach * @param Builder $query 44225d4b688SGreg Roach * @param JewishDate $anniv 44325d4b688SGreg Roach */ 44425d4b688SGreg Roach private function nisanAnniversaries(Builder $query, JewishDate $anniv): void 44525d4b688SGreg Roach { 44625d4b688SGreg Roach if ($anniv->day === 1 && !$anniv->isLeapYear()) { 44725d4b688SGreg Roach $query->where(function (Builder $query): void { 44825d4b688SGreg Roach $query->where(function (Builder $query): void { 44925d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 8); 45025d4b688SGreg Roach })->orWhere(function (Builder $query): void { 45125d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 6); 45225d4b688SGreg Roach }); 45325d4b688SGreg Roach }); 45425d4b688SGreg Roach } else { 45525d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 45625d4b688SGreg Roach } 45725d4b688SGreg Roach } 458f0a11419SGreg Roach} 459