project = $project; $this->year = $year; $this->handle(); } public function getProjectDetails() { return $this->projectDetails; } private function handle() { $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() { 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() { $this->totalProjectPayments += $this->payment->paymentamount; } private function setProjectDetails() { $this->projectDetails = [ 'projectname' => $this->projectname, 'customer' => $this->customer, 'monthPayments' => $this->monthPayments, 'totalProjectPayments' => number_format( $this->totalProjectPayments, 0, ',', '.' ) ]; } }