year = $year; $this->orderType = $orderType; $this->orderDirection = $orderDirection; $this->handle(); } public function getProjectDetails() { return $this->projectDetails; } private function handle() { $this->setProjectsWithPayments(); $this->loopProjectsWithPayments(); } private function setProjectsWithPayments() { $projectsArray = Payment::getProjectsWithPayments( $this->year ); $this->projectsWithPayments = Project::getProjectsForReport( $this->orderType, $this->orderDirection, FALSE, $projectsArray ); } private function loopProjectsWithPayments() { foreach ( $this->projectsWithPayments as $this->project ) { $this->setProjectname(); $this->setCustomer(); $this->setInitialsMonthPayments(); $this->loopPayments(); $this->setProjectDetails(); } } private function setProjectname() { $this->projectname = $this->project->projectname . '(' . $this->project->projectnumber . ')'; } private function setCustomer() { $this->customer = $this->project->customer->name; } private function setInitialsMonthPayments() { for ( $i = 1; $i <= 12; $i ++ ) { $this->monthPayments[ $i ] = ['paymentamount' => 0]; } } private function loopPayments() { $this->totalProjectPayments = 0; foreach ( $this->project->invoices as $invoice ) { foreach ( $invoice->payments as $this->payment ) { $this->setMonthPayments(); $this->setTotalProjectPayments(); } } } private function setMonthPayments() { $paymentYear = date( 'Y', strtotime( $this->payment->paymentdate ) ); $paymentMonth = date( 'n', strtotime( $this->payment->paymentdate ) ); if ( $paymentYear == $this->year ) $this->monthPayments[ $paymentMonth ]['paymentamount'] += $this->payment->paymentamount; } private function setTotalProjectPayments() { $paymentYear = date( 'Y', strtotime( $this->payment->paymentdate ) ); if ( $paymentYear == $this->year ) $this->totalProjectPayments += $this->payment->paymentamount; } private function setProjectDetails() { $this->projectDetails[] = [ 'projectnumber' => $this->project->projectnumber, 'projectname' => $this->projectname, 'customer' => $this->customer, 'monthPayments' => $this->monthPayments, 'totalProjectPayments' => number_format( $this->totalProjectPayments, 0, ',', '.' ) ]; } }