ci =& get_instance(); $this->ci->load->Model( 'projects/Invoices_model' ); $this->handle(); } public function getProjects() { return $this->projects; } private function handle() { $this->setProjects(); $this->setUniqueEmployees(); $this->loopProjects(); $this->setInvoicedYears(); $this->loopProjectsForInvoicedYears(); } private function setProjects() { $this->projects = Project::getActiveProjects()->toArray(); } private function setUniqueEmployees() { $this->uniqueEmployees = Projectemployee::getUniqueProjectEmployees(); } private function loopProjects() { $this->index = 0; foreach ( $this->projects as $this->project ) { $this->projectTotals = 0; $this->setProjectEmployeeDetails(); $this->loopUniqueEmployees(); $this->setBudget(); $this->setProjectCosts(); $this->setBudgetAvailable(); $this->setSaldo(); $this->setInvoiced(); $this->setWorkInProgress(); $this->index ++; } } private function setProjectEmployeeDetails() { $this->projectEmployeeDetails = Projectemployee::getProjectemployeeDetailsPerProject( $this->project['id'] ); } private function loopUniqueEmployees() { foreach ( $this->uniqueEmployees as $this->uniqueEmployee ) { $this->setProjectEmployeeDetail(); $this->setEmployeeBudget(); $this->setEmployeeHours(); $this->setEmployeeDetails(); $this->setProjectTotals(); } } private function setProjectEmployeeDetail() { foreach ( $this->projectEmployeeDetails as $this->projectEmployeeDetail ) { if ( $this->uniqueEmployee->id == $this->projectEmployeeDetail->id ) return; } $this->projectEmployeeDetail = FALSE; } private function setEmployeeBudget() { $this->employeeBudget = is_null( $this->projectEmployeeDetail['budget'] ) ? 0 : $this->projectEmployeeDetail['budget']; } private function setEmployeeHours() { $this->employeeHours = $this->projectEmployeeDetail ? $this->projectEmployeeDetail->hours : 0; } private function setEmployeeDetails() { $this->projects[ $this->index ]['employees'][] = [ 'id' => $this->uniqueEmployee->id, 'fullname' => $this->uniqueEmployee->fullname, 'hours' => $this->employeeHours, 'revenues' => $this->projectEmployeeDetail ? $this->projectEmployeeDetail->revenues : 0, 'budget' => $this->employeeBudget, 'availableHours' => $this->employeeBudget - $this->employeeHours ]; } private function setProjectTotals() { if ( ! isset( $this->projects[ $this->index ]['projectHours'] ) ) $this->projects[ $this->index ]['projectHours'] = 0; if ( ! isset( $this->projects[ $this->index ]['projectEuros'] ) ) $this->projects[ $this->index ]['projectEuros'] = 0; if ( ! isset( $this->projects[ $this->index ]['hoursAvailable'] ) ) $this->projects[ $this->index ]['hoursAvailable'] = 0; $this->projects[ $this->index ]['projectHours'] += $this->projectEmployeeDetail ? $this->projectEmployeeDetail->hours : 0; $this->projects[ $this->index ]['projectEuros'] += $this->projectEmployeeDetail ? $this->projectEmployeeDetail->revenues : 0; $this->projects[ $this->index ]['hoursAvailable'] += $this->employeeBudget - $this->employeeHours; } private function setBudget() { $this->projects[ $this->index ]['budget'] = round( ( $this->projects[ $this->index ]['own_revenue'] + $this->projects[ $this->index ]['other_revenue'] ), 0 ); } private function setProjectCosts() { $this->projects[ $this->index ]['projectcosts'] = Projectcost::getCostsPerPerProject( $this->projects[ $this->index ]['id'] ); } private function setBudgetAvailable() { $this->projects[ $this->index ]['budgetAvailable'] = $this->projects[ $this->index ]['budget'] - $this->projects[ $this->index ]['projectcosts']; } private function setSaldo() { $this->projects[ $this->index ]['saldo'] = $this->projects[ $this->index ]['budgetAvailable'] - $this->projects[ $this->index ]['projectEuros']; } private function setInvoiced() { $invoiced = $this->ci->Invoices_model->getInvoicedPerYearPerProject( $this->projects[ $this->index ]['id'] ); foreach ( $invoiced ? $invoiced : [] as $invoice ) { $this->projects[ $this->index ]['invoiced'][] = [ 'year' => $invoice->invoiceyear, 'invoiced' => $invoice->invoiced ]; $this->setTotalInvoiced( $invoice->invoiced ); } if ( ! isset( $this->projects[ $this->index ]['invoiced'] ) ) { $this->projects[ $this->index ]['invoiced'] = []; $this->projects[ $this->index ]['totalInvoiced'] = 0; } } private function setTotalInvoiced( $invoiced ) { if ( ! isset( $this->projects[ $this->index ]['totalInvoiced'] ) ) $this->projects[ $this->index ]['totalInvoiced'] = 0; $this->projects[ $this->index ]['totalInvoiced'] += $invoiced; } private function setWorkInProgress() { $this->projects[ $this->index ]['workInProgress'] = $this->projects[ $this->index ]['projectEuros'] + $this->projects[ $this->index ]['projectcosts'] - $this->projects[ $this->index ]['totalInvoiced']; if( $this->projects[ $this->index ]['workInProgress'] < 0 ) $this->projects[ $this->index ]['workInProgress'] = 0; } private function setInvoicedYears() { foreach ( $this->projects as $this->project ) { foreach ( $this->project['invoiced'] as $year ) { if ( in_array( $year['year'], $this->invoicedYears ) ) continue; array_push( $this->invoicedYears, $year['year'] ); } } } private function loopProjectsForInvoicedYears() { $this->index = 0; foreach ( $this->projects as $this->project ) { foreach ( $this->invoicedYears as $invoiceYear ) { $hasYear = FALSE; foreach ( $this->project['invoiced'] as $year ) { if ( $invoiceYear == $year['year'] ) $hasYear = TRUE; } if ( ! $hasYear ) $this->projects[ $this->index ]['invoiced'][] = [ 'year' => $invoiceYear, 'invoiced' => 0 ]; } usort( $this->projects[ $this->index ]['invoiced'], function ( $a, $b ) { return $a['year'] - $b['year']; } ); $this->index ++; } } }