Revised database drivers, query builder, and file/inflector helpers for improved consistency and functionality. Updated copyrights, links, and standardized function names including deprecating `is_countable` in favor of `word_is_countable`. Added support for new SQL features and optimized query handling.
286 lines
8.3 KiB
PHP
286 lines
8.3 KiB
PHP
<?php
|
|
/**
|
|
* Created by Pensare Software.
|
|
* User: Theo Weijmer
|
|
* Date: 20/02/2018
|
|
* Time: 16:49
|
|
*/
|
|
|
|
class Recapprojects {
|
|
|
|
private $ci;
|
|
private $index;
|
|
private $field;
|
|
private $archive;
|
|
private $project;
|
|
private $projects;
|
|
private $direction;
|
|
private $employeeHours;
|
|
private $invoicedYears = [];
|
|
private $projectTotals;
|
|
private $employeeBudget;
|
|
private $uniqueEmployee;
|
|
private $uniqueEmployees;
|
|
private $currentFinancialYear;
|
|
private $projectEmployeeDetail;
|
|
private $projectEmployeeDetails;
|
|
|
|
public function __construct( $archive, $field, $direction )
|
|
{
|
|
$this->ci =& get_instance();
|
|
|
|
$this->archive = $archive;
|
|
$this->field = $field;
|
|
$this->direction = $direction;
|
|
|
|
$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(
|
|
$this->archive, $this->field, $this->direction )->toArray();
|
|
}
|
|
|
|
private function setUniqueEmployees()
|
|
{
|
|
$this->uniqueEmployees =
|
|
Projectemployee::getUniqueEmployeesWithActivity();
|
|
}
|
|
|
|
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->setCurrentFinancialYear();
|
|
|
|
$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()
|
|
{
|
|
if( property_exists($this, 'uniqueEmployee') ) return $this->employeeBudget = 0;
|
|
|
|
if( is_null($this->projectEmployeeDetail)) return $this->employeeBudget = 0;
|
|
|
|
$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 setCurrentFinancialYear()
|
|
{
|
|
$this->currentFinancialYear = $this->project['current_financial_year'];
|
|
}
|
|
|
|
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 ++;
|
|
}
|
|
}
|
|
}
|