| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * FiscalHelper.php | 
					
						
							|  |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * This software may be modified and distributed under the terms of the | 
					
						
							|  |  |  |  * Creative Commons Attribution-ShareAlike 4.0 International License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See the LICENSE file for details. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Helpers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Preferences; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class FiscalHelper | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Helpers | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class FiscalHelper implements FiscalHelperInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @var bool */ | 
					
						
							|  |  |  |     protected $useCustomFiscalYear; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * FiscalHelper constructor. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-27 10:38:51 +02:00
										 |  |  |         $this->useCustomFiscalYear = Preferences::get('customFiscalYear', false)->data; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:23:14 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Carbon date object | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function endOfFiscalYear(Carbon $date): Carbon | 
					
						
							| 
									
										
										
										
											2016-02-04 07:23:14 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // get start of fiscal year for passed date
 | 
					
						
							|  |  |  |         $endDate = $this->startOfFiscalYear($date); | 
					
						
							|  |  |  |         if ($this->useCustomFiscalYear === true) { | 
					
						
							|  |  |  |             // add 1 year and sub 1 day
 | 
					
						
							|  |  |  |             $endDate->addYear(); | 
					
						
							|  |  |  |             $endDate->subDay(); | 
					
						
							| 
									
										
										
										
											2016-04-27 10:38:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $endDate; | 
					
						
							| 
									
										
										
										
											2016-02-04 07:23:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-27 10:38:51 +02:00
										 |  |  |         $endDate->endOfYear(); | 
					
						
							| 
									
										
										
										
											2016-02-04 07:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $endDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Carbon date object | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-06 16:37:28 +02:00
										 |  |  |     public function startOfFiscalYear(Carbon $date): Carbon | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  |     { | 
					
						
							|  |  |  |         // get start mm-dd. Then create a start date in the year passed.
 | 
					
						
							|  |  |  |         $startDate = clone $date; | 
					
						
							|  |  |  |         if ($this->useCustomFiscalYear === true) { | 
					
						
							|  |  |  |             $prefStartStr = Preferences::get('fiscalYearStart', '01-01')->data; | 
					
						
							|  |  |  |             list($mth, $day) = explode('-', $prefStartStr); | 
					
						
							|  |  |  |             $startDate->month(intval($mth))->day(intval($day)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // if start date is after passed date, sub 1 year.
 | 
					
						
							|  |  |  |             if ($startDate > $date) { | 
					
						
							|  |  |  |                 $startDate->subYear(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-04-27 10:38:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $startDate; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-27 10:38:51 +02:00
										 |  |  |         $startDate->startOfYear(); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 07:23:14 +01:00
										 |  |  |         return $startDate; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:54:04 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | } |