Files
dbois/application/models/eloquent/Project.php
T
2018-07-29 21:26:49 +02:00

164 lines
3.9 KiB
PHP

<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:05
*/
class Project extends BaseModel {
protected $table = "projects";
public function offers()
{
return $this->hasMany( 'Offer' );
}
public function offer()
{
return $this->hasOne( 'Offer' );
}
public function projectemployees()
{
return $this->hasMany( 'Projectemployee' );
}
public function projectcosts()
{
return $this->hasMany( 'Projectcost' );
}
public function invoices()
{
return $this->hasMany( 'Invoice' );
}
public function forecasts()
{
return $this->hasMany( 'Forecast' );
}
public function reservations()
{
return $this->hasMany( 'Reservation' );
}
public function vat()
{
return $this->belongsTo('Vat');
}
public function projectleader()
{
return $this->belongsTo( 'User', 'projectleader_id' );
}
public function customer()
{
return $this->belongsTo( 'Customer' );
}
public static function getAllProjects( $archive = 'F' )
{
$projects = Project::getBaseProject();
if ( $archive == 'F' ) $projects = $projects->whereArchive( 'F' );
$projects = $projects->get();
return $projects;
}
public static function getProject( $projectID )
{
$project = Project::getBaseProject();
$project = $project->whereArchive( 'F' )
->whereId( $projectID )
->first();
return $project;
}
public static function removeProject( $projectID )
{
Offer::whereProjectId( $projectID )->update( ['project_id' => NULL] );
Project::whereId( $projectID )->delete();
}
public static function getActiveProjects()
{
$projects = Project::selectRaw( "projects.id, YEAR(projects.startdate) AS instroom, YEAR(projects.enddate) AS uitstroom, projectnumber, current_financial_year,
projectname, CUS.name, OFF.own_revenue, OFF.other_revenue" )
->join( 'customers AS CUS', 'CUS.id', '=', 'projects.customer_id' )
->join( 'offers AS OFF', 'OFF.project_id', '=', 'projects.id' )
->groupBy( 'projects.id' )
->groupBy( 'OFF.own_revenue' )
->groupBy( 'OFF.other_revenue' )
->get();
return $projects;
}
public static function getProjectsForReport()
{
$projects = self::getBaseProject()->get();
return $projects;
}
public static function getBaseProject()
{
$project = Project::with( [
'projectleader' => function ( $q ) {
$q->selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" );
}] )
->with( 'offer' )
->with( [
'projectemployees.user' => function ( $q ) {
$q->selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" );
}] )
->with('vat')
->with(['forecasts'])
->with(['reservations'])
->with( 'projectemployees.projecthours' )
->with( 'projectcosts.external' )
->with( 'invoices.payments' )
->with( 'invoices.vat' )
->with( 'vat' )
->with( 'customer' );
return $project;
}
public static function createProject()
{
$data = ['startdate' => date( 'Y-m-d' )];
$project = Project::create( $data );
return $project->id;
}
public static function saveProject( $projectID, $project )
{
$project = (array) $project;
if ( $projectID == 0 )
{
$project = Project::create( $project );
$projectID = $project['id'];;
}
else
{
Project::whereId( $projectID )->update( $project );
}
return $projectID;
}
}