Files
dbois/application/models/eloquent/Project.php
T
2019-10-25 16:52:12 +02:00

198 lines
4.6 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, $search, $order, $direction )
{
$projects = Project::getBaseProject();
$projects = $projects->selectRaw( 'projects.*' )
->join( 'customers AS CUS', 'CUS.id', '=', 'projects.customer_id' )
->join( 'users AS USR', 'USR.id', '=', 'projects.projectleader_id' )
->where( 'projects.projectnumber', 'LIKE', "%{$search}%" )
->orderBy( $order, $direction );
if ( $archive == 'F' ) $projects = $projects->where( 'projects.archive', '=', 'F' );
$projects = $projects->get();
return $projects;
}
public static function getProject( $projectID )
{
$project = Project::getBaseProject();
$project = $project->whereId( $projectID )
// ->whereArchive( 'F' )
->first();
return $project;
}
public static function removeProject( $projectID )
{
Offer::whereProjectId( $projectID )->update( ['project_id' => NULL] );
Project::whereId( $projectID )->delete();
}
public static function getActiveProjects( $archive = 'T', $sortField = FALSE, $direction = FALSE )
{
$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' );
if ( $sortField ) $projects = $projects->orderBy( $sortField, $direction );
if ( $archive == 'F' ) $projects = $projects->where( 'projects.archive', '=', 'F' );
$projects = $projects->get();
return $projects;
}
public static function getProjectsForReport( $orderType, $orderDirection, $archive = 'F', $projectsArray = FALSE )
{
$projects = self::getBaseProject();
if ( $archive == 'F' ) $projects = $projects->whereArchive( 'F' );
if ( is_array( $projectsArray ) ) $projects = $projects->whereIn( 'id', $projectsArray );
$projects = $projects->orderBy( $orderType, $orderDirection )->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;
}
public static function syncOffername( $offer )
{
self::whereId( $offer->project_id )
->update( ['projectname' => $offer->offername] );
}
public static function getProjectsWithExternalCosts( $projects )
{
$projectCosts = self::selectRAW( 'projects.id, OFF.costs' )
->join( 'offers AS OFF', 'OFF.project_id', '=', 'projects.id' )
->whereNotIn( 'projects.id', $projects )
->where( 'OFF.costs', '>', 0 )
->get();
return $projectCosts;
}
}