| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace PhpCfdi\Finkok\Services\Utilities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DateTimeImmutable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class ReportTotalCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $rfc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $startYear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $startMonth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $endYear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $endMonth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $startPeriod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $endPeriod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 17 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         string $rfc, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         string $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         int $startYear, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         int $startMonth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         int $endYear = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         int $endMonth = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 17 |  |         $endYear = $endYear ?: $startYear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 17 |  |         $endMonth = $endMonth ?: $startMonth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 17 |  |         $today = $this->today(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 17 |  |         $currentYear = intval($today->format('Y')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 17 |  |         $currentPeriod = sprintf('%04d-%02d', $currentYear, intval($today->format('m'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 17 |  |         $this->rfc = $rfc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 17 |  |         $this->type = $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 17 |  |         $this->startYear = $startYear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 17 |  |         $this->startMonth = $startMonth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 17 |  |         $this->endYear = $endYear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 17 |  |         $this->endMonth = $endMonth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 17 |  |         $this->startPeriod = sprintf('%04d-%02d', $this->startYear, $this->startMonth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 17 |  |         $this->endPeriod = sprintf('%04d-%02d', $this->endYear, $this->endMonth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 17 |  |         if ($startYear < 2000 || $startYear > $currentYear) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |             throw new LogicException(sprintf('Start year %d is not between 2000 and %d', $startYear, $currentYear)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 15 |  |         if ($endYear < 2000 || $endYear > $currentYear) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             throw new LogicException(sprintf('End year %d is not between 2000 and %d', $endYear, $currentYear)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 13 |  |         if ($startMonth < 1 || $startMonth > 12) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |             throw new LogicException(sprintf('Start month %d is not between 1 and 12', $startMonth)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 11 |  |         if ($endMonth < 1 || $endMonth > 12) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |             throw new LogicException(sprintf('End month %d is not between 1 and 12', $endMonth)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 9 |  |         if ($this->startPeriod > $this->endPeriod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             throw new LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |                 sprintf('Start period %s cannot be greater than end period %s', $this->startPeriod, $this->endPeriod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 8 |  |         if ($this->startPeriod < $this->endPeriod && $this->endPeriod >= $currentPeriod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |             throw new LogicException(sprintf('Cannot combine multiple past months with current/future months')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 3 |  |     public function rfc(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 3 |  |         return $this->rfc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |     public function type(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         return $this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |     public function startPeriod(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |         return $this->startPeriod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |     public function endPeriod(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         return $this->endPeriod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |     public function startString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         return sprintf('%04d-%02d-01T00:00:00', $this->startYear, $this->startMonth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |     public function endString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         $date = new DateTimeImmutable(sprintf('%04d-%02d-01', $this->endYear, $this->endMonth)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |         $today = $this->today(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |         if ($this->endPeriod() === $today->format('Y-m')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |             $date = $today; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |             $date = $date->modify('+ 1 month'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |         return sprintf('%sT00:00:00', $date->format('Y-m-d')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 16 |  |     protected function today(): DateTimeImmutable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 16 |  |         return new DateTimeImmutable('today'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  |