Conversie codeigniter 4

This commit is contained in:
2026-01-09 11:41:28 +01:00
parent dbd8c8aed2
commit f0b9c6ad40
242 changed files with 20034 additions and 346 deletions
+59
View File
@@ -0,0 +1,59 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 25/12/2017
* Time: 10:20
*/
use \Illuminate\Database\Eloquent\Model as Eloquent;
class BaseModel extends Eloquent {
/**
* The name of the "created at" column.
*
* @var string
*/
const CREATED_AT = 'created_by';
/**
* The name of the "updated at" column.
*
* @var string
*/
const UPDATED_AT = 'last_update_date';
protected $guarded = [];
protected $hidden = ['creation_date', 'last_update_date', 'created_by', 'updated_by', 'password', 'salt'];
protected static $oldValue;
public static function boot()
{
parent::boot();
static::creating( function ( $model ) {
static::$oldValue = '';
if ( AUTHENTICATED )
{
$user = User::find(USERID);
$model->created_by = $user->username;
$model->updated_by = $user->username;
}
} );
static::updating( function ( $model ) {
static::$oldValue = json_encode($model->original);
if ( AUTHENTICATED )
{
$user = User::find(USERID);
$model->updated_by = $user->username;
}
} );
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 10/01/2018
* Time: 14:22
*/
class Comment extends BaseModel {
protected $table = "comments";
public function user()
{
return $this->belongsTo( 'User' );
}
public static function getComments( $foreignID, $tablename )
{
$comments = Comment::orderBy( 'comment_date', 'desc' )
->with( [
'user' => function ( $q ) {
$q->selectRaw( "CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname, id" );
}] )
->whereForeignId( $foreignID )
->whereForeignTable( $tablename )
->get();
foreach ( $comments as $comment )
{
$comment->comment = base64_decode( $comment->comment );
}
return $comments;
}
public static function addComment( $foreignID, $tablename )
{
$data = [
'foreign_id' => $foreignID,
'foreign_table' => $tablename,
'user_id' => USERID,
'comment_date' => date( 'Y-m-d H:i:s' )
];
$comment = Comment::create( $data );
return $comment['id'];
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
/**
* Created by Pensare Software.
* Customer: Theo Weijmer
* Date: 21/12/2017
* Time: 18:04
*/
class Customer extends BaseModel {
protected $table = "customers";
public static function getAllCustomers( $archive )
{
$customers = Customer::orderBy( 'name' );
if ( $archive == 'F' ) $customers = $customers->whereArchive( 'F' );
$customers = $customers->get();
return $customers;
}
public static function saveCustomer( $customerID, $customer )
{
$customer = (array)$customer;
if( $customerID == 0)
{
$customer = Customer::create($customer);
$customerID = $customer['id'];
}
else
{
Customer::whereId($customerID)->update($customer);
}
return $customerID;
}
public static function removeCustomer($customerID)
{
Customer::whereId($customerID)->delete();
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 10/01/2018
* Time: 20:02
*/
class Document extends BaseModel {
protected $table = "documents";
public function user()
{
return $this->belongsTo( 'User' );
}
public static function getDocuments( $foreignID)
{
$documents = Document::orderBy( 'uploaddate', 'desc' )
->with( [
'user' => function ( $q ) {
$q->selectRaw( "CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname, id" );
}] )
->whereForeignId( $foreignID )
->get();
return $documents;
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:05
*/
class Employeehour extends BaseModel {
protected $table = "employeehours";
public function user()
{
return $this->belongsTo( 'User', 'employee_id' );
}
public function hourtype()
{
return $this->belongsTo( 'Hourtype' );
}
public static function getEmployeehours( $month, $year )
{
$employeehours = Employeehour::whereRaw( "MONTH(hourdate) = " . ( $month + 1 ) . " AND YEAR(hourdate) = {$year}" )
->with( [
'user' => function ( $q ) {
$q->selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" );
}] )
->with( 'hourtype' );
$projecthours = Employeehour::whereRaw("MONTH(hourdate) = " . ( $month + 1 ) . " AND YEAR(hourdate) = {$year}" )->with( [
'user' => function ( $q ) {
$q->selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" );
}] )
->with( 'hourtype' );
$results = $employeehours->union($projecthours)->get();
return $results;
}
public static function saveEmployeehour( $data )
{
$record = Employeehour::whereUuid( $data['uuid'])->first();
if ( ! $record )
{
$employeehour = Employeehour::create( $data );
$employeehourID = $employeehour->id;
}
else
{
Employeehour::whereUuid( $data['uuid'] )->update( $data );
$employeehourID = FALSE;
}
return $employeehourID;
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/04/2018
* Time: 19:55
*/
class Employeetarget extends BaseModel {
protected $table = 'employeetargets';
public function user()
{
return $this->belongsTo( 'User' );
}
public static function saveTarget( $targetID, $target )
{
$target = (array) $target;
if ( $targetID == 0 )
{
$target = self::create( $target );
$targetID = $target['id'];
}
else
{
self::whereId( $targetID )->update( $target );
}
return $targetID;
}
public static function getTargetHoursPerEmployee( $employeeID, $year, $monthID )
{
$months = $monthID == 0 ? [1,2,3,4,5,6,7,8,9,10,11,12] : [$monthID];
$targetHours = 0;
foreach($months as $month)
{
$targetHours += self::getCurrentMonthTarget( $employeeID, $year, $month);
}
return $targetHours;
}
public static function getCurrentMonthTarget( $employeeID, $year, $month )
{
$monthTarget = Self::whereUserId($employeeID)
->where('startyear', '<=', $year)
->where('startmonth', '<=', $month)
->orderBy('startyear', 'DESC')
->orderBy('startmonth', 'DESC');
if( $monthTarget->count() == 0 ) return 0;
$monthtarget = $monthTarget->first();
return $monthtarget->target;
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
/**
* Created by Pensare Software.
* External: Theo Weijmer
* Date: 21/12/2017
* Time: 18:04
*/
class External extends BaseModel {
protected $table = "externals";
public function reservations()
{
return $this->hasMany( 'Reservation' );
}
public static function getAllExternals( $archive )
{
$externals = External::orderBy( 'name' );
if ( $archive == 'F' ) $externals = $externals->whereArchive( 'F' );
$externals = $externals->get();
return $externals;
}
public static function saveExternal( $externalID, $external )
{
$external = (array) $external;
if ( $externalID == 0 )
{
$external = External::create( $external );
$externalID = $external['id'];
}
else
{
External::whereId( $externalID )->update( $external );
}
return $externalID;
}
public static function removeExternal( $externalID )
{
External::whereId( $externalID )->delete();
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 07/03/2018
* Time: 09:24
*/
class Forecast extends BaseModel {
public function project()
{
return $this->belongsTo( 'Project' );
}
public static function saveForecast( $forecastID, $forecast )
{
$forecast = (array) $forecast;
if ( $forecastID == 0 )
{
$forecast = self::create( $forecast );
$forecastID = $forecast['id'];
}
else
{
self::whereId( $forecastID )->update( $forecast );
}
return $forecastID;
}
public static function removeForecast( $forecastID )
{
Forecast::whereId( $forecastID )->delete();
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:05
*/
class Hourtype extends BaseModel {
protected $table = "hourtypes";
public function employeehours()
{
return $this->hasMany( 'Employeehour' );
}
}
+147
View File
@@ -0,0 +1,147 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 23/01/2018
* Time: 15:58
*/
class Invoice extends BaseModel {
public function project()
{
return $this->belongsTo( 'Project' );
}
public function payments()
{
return $this->hasMany( 'Payment' );
}
public function vat()
{
return $this->belongsTo( 'Vat' );
}
public static function invoicePerYear( $projectID, $year, $thatYear = TRUE )
{
$invoices = Invoice::selectRAW( 'SUM(invoiceamount * ( 1 +(vatrate / 100))) AS invoicetotal' )
->join( 'vats', 'vats.id', '=', 'invoices.vat_id' )
->whereProjectId( $projectID );
if ( $thatYear )
{
$invoices = $invoices->whereRaw( "YEAR(invoicedate) = {$year}" );
}
else
{
$invoices = $invoices->whereRaw( "YEAR(invoicedate) != {$year}" );
}
if ( $invoices->count() == 0 ) return 0;
$invoices = $invoices->first();
return $invoices->invoicetotal;
}
public static function saveInvoice( $invoiceID, $invoice )
{
$invoice = (array) $invoice;
if ( $invoiceID == 0 )
{
$invoice = Invoice::create( $invoice );
$invoiceID = $invoice['id'];
}
else
{
Invoice::whereId( $invoiceID )->update( $invoice );
}
return $invoiceID;
}
public static function getInvoiced( $year, $orderType='invoicedate', $orderDirection='DESC' )
{
$invoiced = Invoice::selectRaw('invoicenumber, PRJ.projectname, projectnumber, CUS.name, invoicedate, CUS.name, ROUND(invoiceamount, 0) AS invoiceamount')
->join('projects AS PRJ', 'PRJ.id', '=', 'invoices.project_id')
->join('customers AS CUS', 'CUS.id', '=', 'PRJ.customer_id')
->whereRaw("DATE_FORMAT(invoicedate, '%Y') = {$year}")
->orderBy($orderType, $orderDirection)
->get();
return $invoiced;
}
public static function getInvoicedPerYearPerProject($projectID)
{
$invoiced = Invoice::selectRaw('ROUND(COALESCE(SUM(invoiceamount), 0), 0) AS invoiced, YEAR(invoicedate) AS invoiceyear')
->where('project_id', $projectID)
->groupByRaw('YEAR(invoicedate)')
->orderByRaw('YEAR(invoicedate)')
->get();
return $invoiced;
}
public static function getTotalsOutstandingPerProject($archive = true)
{
$sql = "
SELECT
PRJ.id,
projectnumber,
projectname,
SUM(invoiced) AS invoiced,
SUM(payment) AS payment,
revenues,
costs,
COALESCE(payedcosts, 0) AS payedcosts
FROM (
SELECT
project_id,
ROUND(SUM(invoiceamount * (1 + (vatrate / 100))), 0) AS invoiced,
payment
FROM
invoices INV
JOIN vats VAT ON VAT.id = INV.vat_id
LEFT JOIN (
SELECT
ROUND(COALESCE(SUM(paymentamount), 0), 0) AS payment,
invoice_id
FROM
payments
GROUP BY
invoice_id) AS Q1 ON INV.id = Q1.invoice_id
GROUP BY
INV.id) Q2
JOIN projects PRJ ON PRJ.id = Q2.project_id
JOIN (
SELECT
ROUND((own_revenue + other_revenue + costs) * (1 + (vatrate / 100)), 0) revenues,
ROUND(costs * (1 + (vatrate / 100)), 0) costs,
OFF.project_id
FROM
offers OFF
JOIN projects PRJ ON PRJ.id = OFF.project_id
JOIN vats VAT ON VAT.id = PRJ.vat_id) Q3 ON Q3.project_id = PRJ.id
LEFT JOIN (
SELECT
SUM(costs) AS payedcosts,
project_id
FROM
projectcosts
GROUP BY
project_id) Q4 ON Q4.project_id = PRJ.id ";
if ($archive === false) {
$sql .= "WHERE PRJ.archive = 'F' ";
}
$sql .= "GROUP BY PRJ.id";
$db = \Config\Database::connect();
return $db->query($sql)->getResult();
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:05
*/
class Offer extends BaseModel {
protected $table = "offers";
public function payments()
{
return $this->hasMany( 'Payment' );
}
public function project()
{
return $this->belongsTo( 'Project' );
}
public function customer()
{
return $this->belongsTo( 'Customer' );
}
public static function getAllOffers( $projectID, $archive = 'F', $search, $order, $direction )
{
$offers = Offer::selectRAW( 'offers.*' )
->join( 'customers AS CUS', 'CUS.id', '=', 'offers.customer_id' )
->with( 'customer' )
->with( 'project' )
->where( 'offernumber', 'LIKE', "%{$search}%" )
->orderBy( $order, $direction );
if ( $projectID ) $offers = $offers->whereProjectId( $projectID );
if ( $archive == 'F' ) $offers = $offers->where( 'offers.archive', '=', 'F' );
$offers = $offers->get();
return $offers;
}
public static function getOffersWithoutProject()
{
$offersWithoutProject = Offer::selectRAW('offers.*, CONCAT(offernumber, " ", offername) AS fulloffer')
->orderBy( 'startdate' )
// ->whereNull('rejectdate')
// ->whereNull('project_id')
->whereArchive( 'F' )
->get();
return $offersWithoutProject;
}
public static function saveOffer( $offerID, $offer )
{
$offer = (array) $offer;
$offer['startdate'] = date2mysql( $offer['startdate'] );
$offer['accepteddate'] = date2mysql( $offer['accepteddate'] );
$offer['rejectdate'] = date2mysql( $offer['rejectdate'] );
$offer['deadline'] = date2mysql( $offer['deadline'] );
if ( $offerID == 0 )
{
$offer = Offer::create( $offer );
$offerID = $offer['id'];
}
else
{
Offer::whereId( $offerID )->update( $offer );
$offer = self::find( $offerID );
if ( ! is_null( $offer->project_id ) ) Project::syncOffername( $offer );
}
return $offerID;
}
public static function syncProjectname( $projectID )
{
$project = Project::find( $projectID );
$offer = self::find( $projectID );
$offer->offername = $project->projectname;
$offer->save();
}
public static function removeOffer( $offerID )
{
Offer::whereId( $offerID )->delete();
}
}
+62
View File
@@ -0,0 +1,62 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:05
*/
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Page extends BaseModel {
protected $table = "pages";
public function subpages()
{
return $this->hasMany( 'Page' );
}
public static function getPages( $user )
{
$pages = Page::whereRaw( "id = page_id AND ((SELECT {$user->role} & pagelevel) != 0 OR pagelevel = 0)" )
->with( [
'subpages' => function ( $q ) use ( $user ) {
$q->whereRaw(
"id != page_id AND ((SELECT {$user->role} & pagelevel) != 0 OR pagelevel = 0)" )
->orderBy( 'pageorder' );
}] )
->orderBy( 'pageorder' )
->get();
return $pages;
}
public static function getAppPages( $user )
{
$appPages = Page::select( 'id', 'pageurl' )
->whereAppPage( 'T' )
->whereRaw(
"((SELECT {$user->role} & pagelevel) != 0 OR pagelevel = 0)" )
->get();
return $appPages;
}
public static function getPageAccessByRoleLevel( $roleLevel )
{
$rolePages = self::selectRaw( "
id,
{$roleLevel} AS rolelevel,
IF(id = page_id, 'MAIN', 'SUB') AS typepage,
pagename,
IF((SELECT {$roleLevel} & pagelevel ) = 0, 'F', 'T') AS pageaccess" )
->orderByRaw( "page_id, IF(id = page_id, 0, 1)" )
->get();
return $rolePages;
}
}
+112
View File
@@ -0,0 +1,112 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 16/01/2018
* Time: 10:27
*/
class Payment extends BaseModel {
protected $table = "payments";
public function invoice()
{
return $this->belongsTo( 'Invoice' );
}
public static function getPaymentPerProject( $projectID )
{
$projectPayment = self::selectRAW( 'id, invoice_id, SUM(paymentamount) as payment' )
->whereHas( 'invoice', function ( $q ) use ( $projectID ) {
$q->whereProjectId( $projectID );
} )
->first();
if ( ! $projectPayment ) return 0;
return $projectPayment->payment;
}
public static function getPaymentsPerCustomerMonth( $year )
{
$projects = self::selectRaw( 'PRJ.id, projectname, CUS.name, SUM(paymentamount) AS totalProjectPayments' )
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
->join( 'projects AS PRJ', 'PRJ.id', '=', 'INV.project_id' )
->join( 'customers AS CUS', 'CUS.id', '=', 'PRJ.customer_id' )
->groupBy( 'PRJ.id' )
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = ' . $year )
->get()
->toArray();
for ( $x = 0; $x < count( $projects ); $x ++ )
{
$projects[ $x ]['monthPayments'] = [];
$monthPayments = self::getMonthPaymentPerProject( $projects[ $x ]['id'], $year );
for ( $i = 1; $i <= 12; $i ++ )
{
$projects[ $x ]['monthPayment'][ $i ] = 0;
foreach ( $monthPayments as $monthPayment )
{
if ( $monthPayment->month == $i )
$projects[ $x ]['monthPayment'][ $i ] = $monthPayment->paymentamount;
}
}
}
return $projects;
}
public static function getMonthPaymentPerProject( $projectID, $year )
{
return self::selectRaw( 'SUM(paymentamount) AS paymentamount, DATE_FORMAT(paymentdate, "%m") AS month' )
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = ' . $year )
->whereProjectId( $projectID )
->groupBy( \DB::raw( 'DATE_FORMAT(paymentdate, "%m")' ) )
->get();
}
public static function getProjectsWithPayments( $year )
{
$projects = self::selectRaw( 'INV.project_id' )
->distinct()
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = "' . $year . '"' )
->pluck( 'project_id' );
return $projects->toArray();
}
public static function getPaymentInYear( $year )
{
$payments = self::selectRAW( 'SUM(paymentamount) AS totalpayment' )
->whereRaw( "DATE_FORMAT(paymentdate, '%Y') = '{$year}'" )
->first();
return $payments->totalpayment;
}
public static function savePayment( $paymentID, $payment )
{
$payment = (array) $payment;
if ( $paymentID == 0 )
{
$payment = Payment::create( $payment );
$paymentID = $payment['id'];
}
else
{
Payment::whereId( $paymentID )->update( $payment );
}
return $paymentID;
}
public static function removePayment( $paymentID )
{
Payment::whereId( $paymentID )->delete();
}
}
+197
View File
@@ -0,0 +1,197 @@
<?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;
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 23/01/2018
* Time: 11:37
*/
class Projectcost extends BaseModel {
public function project()
{
return $this->belongsTo( 'Project' );
}
public function external()
{
return $this->belongsTo( 'External' );
}
public static function saveProjectcost( $projectID, $project )
{
$project = (array) $project;
if ( $projectID == 0 )
{
$project = Self::create( $project );
$projectID = $project['id'];
}
else
{
Self::whereId( $projectID )->update( $project );
}
return $projectID;
}
public static function getCostsPerPerProject( $projectID )
{
$projectCosts = Projectcost::selectraw( 'ROUND(COALESCE(SUM(costs), 0), 0) AS costs')
->whereProjectId($projectID)
->first();
return $projectCosts->costs;
}
}
+119
View File
@@ -0,0 +1,119 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 17/01/2018
* Time: 10:25
*/
class Projectemployee extends BaseModel {
public function project()
{
return $this->belongsTo( 'Project' );
}
public function user()
{
return $this->belongsTo( 'User' );
}
public function projecthours()
{
return $this->hasMany( 'Projecthour' );
}
public static function getAvailableEmployees( $projectID )
{
$currentEmployees = Self::whereProjectId( $projectID )
->pluck( 'user_id' );
$availableEmployees = User::selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" )
->whereNotIn( 'id', $currentEmployees )
->whereArchive( 'F' )
->get();
return $availableEmployees;
}
public static function getUniqueProjectEmployees()
{
$projectemployees = Projectemployee::selectRaw( "USR.id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" )
->distinct()
->join( 'users AS USR', 'USR.id', '=', 'projectemployees.user_id' )
->get();
return $projectemployees;
}
public static function getUniqueEmployeesWithActivity( $archive = 'F' )
{
$projectemployees = Projectemployee::selectRaw( "USR.id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" )
->distinct()
->join( 'users AS USR', 'USR.id', '=', 'projectemployees.user_id' )
->join( 'projectemployees AS PRE', 'PRE.user_id', '=', 'USR.id' )
->join( 'projects AS PRJ', 'PRJ.id', '=', 'PRE.project_id' );
if ( $archive == 'F' ) $projectemployees = $projectemployees->where( 'PRJ.archive', '=', 'F' );
$projectemployees = $projectemployees->get();
return $projectemployees;
}
public static function getProjectemployeeDetailsPerProject( $projectID )
{
$details = Projectemployee::selectRaw( '
USR.id, budget, project_id, ROUND(SUM(hours), 0) AS hours, ROUND(SUM(hours * tarif), 0) AS revenues, projectemployees.id AS projectemployeeID' )
->join( 'users AS USR', 'USR.id', '=', 'projectemployees.user_id' )
->join( 'projecthours AS PRH', 'PRH.projectemployee_id', '=', 'projectemployees.id' )
->where( 'projectemployees.project_id', '=', $projectID )
->where( 'PRH.hourtype_id', '=', 1 )
// ->where( 'budget', '!=', 0 )
->groupBy( 'projectemployees.id' )
->havingRaw( 'ROUND(SUM(hours), 0) > 0' );
$details = $details->get();
return $details;
}
public static function firstOrCreateProjectemployee( $userID, $projectID )
{
$projectemployee = self::whereUserId( $userID )->whereProjectId( $projectID );
if ( $projectemployee->count() > 0 ) return $projectemployee->first();
$user = User::find( $userID );
$tarif = $user ? $user->hourlyrate : 0;
$projectemployee = Projectemployee::create( [
'project_id' => $projectID,
'user_id' => $userID,
'tarif' => $tarif
] );
return $projectemployee;
}
public static function createProjectEmployee( $projectID )
{
$projectemployee = Projectemployee::create( [
'project_id' => $projectID
] );
return $projectemployee->id;
}
public static function saveProjectemployee( $projectemployeeID, $projectemployee )
{
Projectemployee::whereId( $projectemployeeID )
->update( $projectemployee );
}
public static function removeProjectemployee( $projectemployeeID )
{
Projectemployee::whereId( $projectemployeeID )->delete();
}
}
+83
View File
@@ -0,0 +1,83 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 19/01/2018
* Time: 15:51
*/
class Projecthour extends BaseModel {
protected $table = "projecthours";
public function projectemployee()
{
$this->belongsTo( 'Projectemployee' );
}
public static function getUsedYears()
{
$years = Projecthour::selectRaw( "YEAR(hourdate) AS year" )
->distinct()
->orderByRaw( 'YEAR( hourdate )' )
->get();
return $years;
}
public static function getMonthlyEmployeeHoursPerProject( $employeeID, $year )
{
$projectHours = ProjecthoursPerEmployeePerMonth::fire( $employeeID, $year );
return $projectHours;
}
public static function recapProjects()
{
$recaps = QRecapProjects::fire();
return $recaps;
}
public static function saveProjecthour( $projecthour )
{
$projecthour = (array) $projecthour;
$record = Projecthour::whereUuid( $projecthour['uuid'] )->first();
if ( ! $record )
{
$projecthour = Projecthour::create( $projecthour );
$projecthourID = $projecthour['id'];
}
else
{
Projecthour::whereUuid( $projecthour['uuid'] )->update( $projecthour );
$projecthourID = FALSE;
}
return $projecthourID;
}
public static function removeProjecthour( $projecthourID )
{
Projecthour::whereId( $projecthourID )->delete();
}
public static function getTotalHoursPerProject( $projectID )
{
$totalProjecthours =
self::selectRaw( "CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
SUM(IF(hourtype_id = 1, hours, 0)) AS declarable,
SUM(IF(hourtype_id = 1, hours * tarif, 0)) AS revenues,
SUM(IF(hourtype_id != 1, hours, 0)) AS nondeclarable" )
->join( 'projectemployees AS PRE', 'PRE.id', '=', 'projecthours.projectemployee_id' )
->join( 'users AS USR', 'USR.id', '=', 'PRE.user_id' )
->groupBy( 'USR.id' )
->where( 'PRE.project_id', '=', $projectID )
->get();
return $totalProjecthours;
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 07/03/2018
* Time: 09:24
*/
class Reservation extends BaseModel {
public function project()
{
return $this->belongsTo( 'Project' );
}
public function external()
{
return $this->belongsTo( 'External' );
}
public static function saveReservation( $reservationID, $reservation )
{
$reservation = (array) $reservation;
if ( $reservationID == 0 )
{
$reservation = self::create( $reservation );
$reservationID = $reservation['id'];
}
else
{
self::whereId( $reservationID )->update( $reservation );
}
return $reservationID;
}
public static function removeReservation( $reservationID )
{
Reservation::whereId( $reservationID )->delete();
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 25/12/2017
* Time: 17:07
*/
class Role extends BaseModel {
protected $table = "roles";
public function user()
{
return $this->hasOne( 'User', 'role', 'rolelevel' );
}
public static function getAllRoles( $archive )
{
$roleHierarchy = Role::getUserroleHierachy();
$roles = Role::orderBy( 'rolehierarchy' )
->where( 'rolehierarchy', '>=', $roleHierarchy );
if ( $archive == 'F' ) $roles = $roles->whereArchive( 'F' );
$roles = $roles->get();
return $roles;
}
public static function getUserroleHierachy()
{
$userrole = User::getUserRoleHierarchy();
if( $userrole == 0 ) return 0;
$roleHierarchy = Role::whereRolelevel($userrole)
->first()
->rolehierarchy;
return $roleHierarchy;
}
public static function getPagesAccessByRole()
{
$roles = self::all();
$pageroles=[];
foreach ( $roles as $role )
{
$pageroles[ $role->id ]['rolelevel'] = $role->rolelevel;
$pageroles[ $role->id ]['rolename'] = $role->rolename;
$pageroles[ $role->id ]['pagesAccess'] =
Page::getPageAccessByRoleLevel( $role->rolelevel );
}
return $pageroles;
}
}
+154
View File
@@ -0,0 +1,154 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 21/12/2017
* Time: 18:04
*/
class User extends BaseModel {
protected $table = "users";
public function projectemployees()
{
return $this->hasMany( 'Projectemployee' );
}
public function employeetargets()
{
return $this->hasMany( 'Employeetarget' );
}
public function projectemployee()
{
return $this->hasOne( 'Projectemployee' );
}
public function role()
{
return $this->belongsTo( 'Role', 'level', 'rolelevel' );
}
public static function getAllUsers( $archive )
{
$users = User::selectRaw( "*, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" )
->orderBy( 'lastname' );
if ( $archive == 'F' ) $users = $users->whereArchive( 'F' );
$users = $users->get();
return $users;
}
public static function getUserRoleHierarchy()
{
$userrole = User::find( USERID )->role;
return $userrole;
}
public static function getUserWithprojects()
{
$usersWithProjects =
User::selectRaw( "users.id, CONCAT(users.firstname, ' ', users.middlename, ' ', users.lastname) AS fullname" )
->with( [
'projectemployees.project' => function ( $q ) {
$q->whereArchive( 'F' );
}] )
->get();
return $usersWithProjects;
}
public static function getUserActiveProjects( $userID )
{
$usersActiveProjects =
User::selectRaw( "PRJ.id AS projectID, projectname, projectnumber, CONCAT(projectnumber, '-', projectname) AS projectFull" )
->join( 'projectemployees AS PRE', 'PRE.user_id', '=', 'users.id' )
->join( 'projects AS PRJ', 'PRJ.id', '=', 'PRE.project_id' )
->where( 'PRJ.archive', '=', 'F' )
->where( 'users.id', '=', $userID )
->orderBy('projectnumber')
->get();
return $usersActiveProjects;
}
public static function checkUsername( $username )
{
$username = strtoupper( $username );
$user = User::whereRaw( "UCASE(username) = {$username}" );
if ( $user->count() == 0 ) return FALSE;
return TRUE;
}
public static function saveUser( $userID, $user )
{
$user = (array) $user;
if ( $userID == 0 )
{
$user = User::create( $user );
$userID = $user['id'];
}
else
{
User::whereId( $userID )->update( $user );
}
return $userID;
}
public static function removeUser( $userID )
{
User::whereId( $userID )->delete();
}
public static function getUniqueProjectUsers()
{
//TODO: date range
$projectusers = User::selectRaw( "
users.id,
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
SUBSTR(MD5(users.id), 1, 6) AS slug" )
->join( 'projectemployees AS PEM', 'PEM.user_id', '=', 'users.id' )
->with( [
'projectemployee.project' => function ( $q ) {
$q->selectRaw( "id, YEAR(startdate) AS instroom, COALESCE(YEAR(enddate), '') AS uitstroom" );
}] )
->distinct()
->get();
return $projectusers;
}
public static function getSaltedPassword( $password, $salt )
{
$saltedpassword = sha1( sha1( $password ) . $salt );
return $saltedpassword;
}
public static function generateSalt()
{
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$alphaLength = strlen( $alphabet ) - 1;
$randomString = '';
for ( $i = 0; $i < 18; $i++ )
{
$n = rand( 0, $alphaLength );
$randomString .= $alphabet[ $n ];
}
return sha1( $randomString );
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
/**
* User OTP Model
* Manages email-based one-time passwords for two-factor authentication
*/
class UserOtp_model
{
protected static string $tablename = "user_otp_codes";
/**
* Generate and store a new OTP code for a user
* @param int $userId User ID
* @param int $expiryMinutes Expiry time in minutes (default 10)
* @return string The generated OTP code
*/
public static function generateOTP($userId, $expiryMinutes = 10)
{
$db = \Config\Database::connect();
// Generate 6-digit OTP
$otpCode = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
// Calculate expiry time
$expiresAt = date('Y-m-d H:i:s', strtotime("+{$expiryMinutes} minutes"));
// Store OTP in database
$data = [
'user_id' => $userId,
'otp_code' => $otpCode,
'expires_at' => $expiresAt,
'used' => 'F',
'created_at' => date('Y-m-d H:i:s')
];
$db->table(self::$tablename)->insert($data);
return $otpCode;
}
/**
* Verify an OTP code for a user
* @param int $userId User ID
* @param string $otpCode The OTP code to verify
* @return bool True if valid, false otherwise
*/
public static function verifyOTP($userId, $otpCode)
{
$db = \Config\Database::connect();
// Get the most recent unused OTP for this user
$result = $db->table(self::$tablename)
->where('user_id', $userId)
->where('otp_code', $otpCode)
->where('used', 'F')
->where('expires_at >', date('Y-m-d H:i:s'))
->orderBy('created_at', 'DESC')
->limit(1)
->get()
->getRow();
if (!$result) {
return false;
}
// Mark OTP as used
$db->table(self::$tablename)
->where('id', $result->id)
->update(['used' => 'T']);
return true;
}
/**
* Clean up expired OTP codes
* @return int Number of deleted records
*/
public static function cleanupExpiredOTPs()
{
$db = \Config\Database::connect();
$db->table(self::$tablename)
->where('expires_at <', date('Y-m-d H:i:s'))
->orWhere('used', 'T')
->delete();
return $db->affectedRows();
}
/**
* Delete all OTP codes for a user
* @param int $userId User ID
*/
public static function deleteUserOTPs($userId)
{
$db = \Config\Database::connect();
$db->table(self::$tablename)
->where('user_id', $userId)
->delete();
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 23/01/2018
* Time: 19:37
*/
class Vat extends BaseModel {
public function invoices()
{
return $this->hasMany( 'Invoice' );
}
public static function getVats( $archive )
{
if ( $archive == 'F' )
{
$vats = vat::whereArchive( 'F' )->get();
}
else
{
$vats = Vat::get();
}
return $vats;
}
public static function getCurrentVat()
{
$currentVat = Vat::whereActive('T')->whereArchive('F')->first();
if( ! $currentVat) return 0;
return $currentVat->vatrate;
}
}
@@ -0,0 +1,85 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 15/03/2018
* Time: 10:47
*/
class EmployeehoursMonthly {
public static function fire($month, $year, $employeeID)
{
$month = $month + 1;
$sql = "
SELECT *
FROM (
SELECT CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
hourtype_id,
EMH.id,
EMH.uuid,
USR.id AS employee_id,
hours,
DATE_FORMAT(hourdate, '%d-%m-%Y') AS hourdate,
NULL AS project_id,
NULL AS projectname,
'F' AS archive,
'OVERHEAD' AS type
FROM employeehours EMH
JOIN users USR
ON USR.id = EMH.employee_id
WHERE MONTH(hourdate) = {$month}
AND YEAR(hourdate) = {$year} ";
if( $employeeID != 0 ) $sql.="AND USR.id = {$employeeID} ";
$sql.="
UNION
SELECT CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
(hourtype_id + 8) AS hourtype_id,
PRH.id,
PRH.uuid,
USR.id AS employee_id,
hours,
DATE_FORMAT(hourdate, '%d-%m-%Y') AS hourdate,
PEM.project_id,
CONCAT( projectnumber, '-',PRJ.projectname),
PRJ.archive,
'PROJECTS' AS type
FROM projecthours PRH
JOIN projectemployees PEM
ON PEM.id = PRH.projectemployee_id
JOIN projects PRJ
ON PRJ.id = PEM.project_id
JOIN users USR
ON USR.id = PEM.user_id
WHERE MONTH(hourdate) = {$month}
AND YEAR(hourdate) = {$year}
AND offer_id IS NULL ";
if( $employeeID != 0 ) $sql.="AND USR.id = {$employeeID} ";
$sql.="
UNION
SELECT CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
(hourtype_id + 8) AS hourtype_id,
PRH.id,
PRH.uuid,
USR.id AS employee_id,
hours,
DATE_FORMAT(hourdate, '%d-%m-%Y') AS hourdate,
offer_id,
CONCAT(OFF.offernumber, '-',OFF.offername),
OFF.archive,
'OFFERS' AS type
FROM projecthours PRH
JOIN offers OFF
ON OFF.id = PRH.offer_id
JOIN users USR
ON USR.id = PRH.projectemployee_id
WHERE MONTH(hourdate) = {$month}
AND YEAR(hourdate) = {$year}
AND offer_id IS NOT NULL ";
if( $employeeID != 0 ) $sql.="AND USR.id = {$employeeID} ";
$sql.=" ) Q1
ORDER BY fullname, hourdate ";
return $sql;
}
}
@@ -0,0 +1,59 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 20/02/2018
* Time: 09:36
*/
class ProjecthoursPerEmployeePerMonth extends BaseModel {
public static function fire($employeeID, $year)
{
$projectHours = Projecthour::selectRaw( "
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-1', projecthours.hours, 0)) AS januari,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-2', projecthours.hours, 0)) AS februari,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-3', projecthours.hours, 0)) AS maart,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-4', projecthours.hours, 0)) AS april,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-5', projecthours.hours, 0)) AS mei,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-6', projecthours.hours, 0)) AS juni,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-7', projecthours.hours, 0)) AS juli,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-8', projecthours.hours, 0)) AS augustus,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-9', projecthours.hours, 0)) AS september,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-10', projecthours.hours, 0)) AS oktober,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-11', projecthours.hours, 0)) AS november,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-12', projecthours.hours, 0)) AS december,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-1', projecthours.hours * tarif, 0)) AS januariRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-2', projecthours.hours * tarif, 0)) AS februariRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-3', projecthours.hours * tarif, 0)) AS maartRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-4', projecthours.hours * tarif, 0)) AS aprilRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-5', projecthours.hours * tarif, 0)) AS meiRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-6', projecthours.hours * tarif, 0)) AS juniRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-7', projecthours.hours * tarif, 0)) AS juliRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-8', projecthours.hours * tarif, 0)) AS augustusRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-9', projecthours.hours * tarif, 0)) AS septemberRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-10', projecthours.hours * tarif, 0)) AS oktoberRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-11', projecthours.hours * tarif, 0)) AS novemberRevenue,
SUM(IF(CONCAT(YEAR(hourdate), '-', MONTH(hourdate)) = '{$year}-12', projecthours.hours * tarif, 0)) AS decemberRevenue,
SUM(IF(YEAR(hourdate) = {$year},projecthours.hours, 0)) AS totalyear,
SUM(IF(YEAR(hourdate) != {$year},projecthours.hours, 0)) AS totalotheryears,
SUM(IF(YEAR(hourdate) = {$year},projecthours.hours * tarif, 0)) AS totalyearRevenue,
SUM(IF(YEAR(hourdate) != {$year},projecthours.hours * tarif, 0)) AS totalotheryearsRevenue,
SUM(projecthours.hours) AS totalhours,
SUM(projecthours.hours * tarif) AS totalhoursRevenue,
PEM.tarif,
PRJ.projectname" )
->join( 'projectemployees AS PEM', 'PEM.id', '=', 'projecthours.projectemployee_id' )
->join( 'projects AS PRJ', 'PRJ.id', '=', 'PEM.project_id' )
->where( 'PEM.user_id', '=', $employeeID )
->where( 'projecthours.hourtype_id', '=', 1)
->groupBy('PRJ.id')
->groupBy('PEM.tarif')
->groupBy('PRJ.projectname')
->get();
return $projectHours;
}
}
@@ -0,0 +1,80 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 01/03/2018
* Time: 12:03
*/
class QEmployeeHours {
public static function fire( $month, $year )
{
$sql = "
SELECT monthID,
user_id,
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
ROUND(SUM(projecthours), 0) AS projecthours,
ROUND(SUM(profitablehours), 0) AS profitablehours,
ROUND(SUM(nonprofitablehours), 0) AS nonprofitablehours,
ROUND(SUM(revenues), 0) AS revenues,
ROUND(SUM(nonprojecthours), 0) AS nonprojecthours,
ROUND(SUM(projecthours + nonprojecthours), 0) AS totalhours,
targethours
FROM (
SELECT employee_id AS user_id,
MONTH(hourdate) monthID,
0 AS projecthours,
0 AS profitablehours,
0 AS nonprofitablehours,
0 AS revenues,
SUM(hours) AS nonprojecthours
FROM employeehours EMH
JOIN hourtypes HTY
ON HTY.id = EMH.hourtype_id
WHERE YEAR(hourdate) = '{$year}'
AND reportable = 'T' ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY employee_id, MONTH(hourdate)
UNION
SELECT PEM.user_id,
MONTH(hourdate) monthID,
SUM(hours) AS projecthours,
SUM(IF(hourtype_id = 1, hours, 0)) AS profitablehours,
SUM(IF(hourtype_id = 2, hours, 0)) AS nonprofitablehours,
ROUND(SUM( IF(hourtype_id = 1, (hours * PEM.tarif), 0) ), 0) AS revenues,
0 AS nonprojecthours
FROM projecthours PH
JOIN projectemployees PEM
ON PEM.id = PH.projectemployee_id
WHERE YEAR(hourdate) = '{$year}'
AND hourtype_id <= 2 ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY PEM.user_id, MONTH(hourdate)
UNION
SELECT PH.projectemployee_id AS user_id,
MONTH(hourdate) monthID,
SUM(hours) AS projecthours,
0 AS profitablehours,
SUM(IF(hourtype_id = 3, hours, 0)) AS nonprofitablehours,
0 AS revenues,
0 AS nonprojecthours
FROM projecthours PH
WHERE YEAR(hourdate) = '{$year}'
AND hourtype_id = 3 ";
if( $month != 0 ) $sql .= "AND MONTH(hourdate) = '$month' ";
$sql .= "
GROUP BY PH.projectemployee_id, MONTH(hourdate)
) AS Q1
JOIN users AS USR
ON USR.id = Q1.user_id
GROUP BY user_id ";
return $sql;
}
}
@@ -0,0 +1,12 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 20/02/2018
* Time: 09:45
*/
class QRecapProjects {
}
@@ -0,0 +1,43 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 07/03/2018
* Time: 15:31
*/
class QinvoiceMonthOverview {
public static function fire( $projectID, $year )
{
$sql = "SELECT MNT.id,
month,
COALESCE(forecastamount, 0) AS forecastamount,
COALESCE(paymentamount, 0) AS paymentamount
FROM months MNT
LEFT JOIN (
SELECT SUM(forecastamount) AS forecastamount, month_id
FROM forecasts
WHERE project_id = {$projectID}
AND `year` = {$year}
GROUP BY month_id
) AS FCT
ON MNT.id = FCT.month_id + 1
LEFT JOIN (
SELECT SUM(paymentamount) AS paymentamount, MONTH(paymentdate) AS month_id
FROM payments PAY
JOIN invoices INV
ON INV.id = PAY.invoice_id
WHERE INV.project_id = {$projectID}
AND YEAR(paymentdate) = {$year}
GROUP BY month_id
) AS PAY
ON MNT.id = PAY.month_id
LEFT JOIN projects PRJ
GROUP BY MNT.id
ORDER BY MNT.id ";
return $sql;
}
}
@@ -0,0 +1,66 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 02/03/2018
* Time: 15:56
*/
class Qinvoiceoverview {
public static function fire( $year )
{
$sql = "
SELECT PRJ.id, PRJ.projectnumber,
PRJ.projectname,
(own_revenue + other_revenue + OFF.costs ) AS revenues,
CUS.name AS customername,
SUM(IF( YEAR(invoicedate) = {$year}, invoiceamount* ((100 + VTS.vatrate)/100), 0)) AS currentyear,
SUM(IF( YEAR(invoicedate) != {$year}, invoiceamount * ((100 + VTS.vatrate)/100), 0)) AS otheryear,
SUM(COALESCE(Q1.currentyearpayed, 0)) AS currentyearpayed,
SUM(COALESCE(Q1.otheryearpayed, 0)) AS otheryearpayed,
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
( SELECT vatrate FROM vats WHERE archive = 'F' LIMIT 1 ) AS vatrate,
COALESCE(reserved, 0 ) AS reserved,
COALESCE(currentforecast, 0) AS currentforecast,
COALESCE(otherforecast, 0) AS otherforecast
FROM invoices INV
RIGHT JOIN projects PRJ
ON PRJ.id = INV.project_id
JOIN offers OFF
ON PRJ.id = OFF.project_id
JOIN customers CUS
ON CUS.id = PRJ.customer_id
JOIN users USR
ON USR.id = PRJ.projectleader_id
JOIN vats VTS
ON VTS.id = INV.vat_id
LEFT JOIN (
SELECT invoice_id,
SUM(IF(YEAR(paymentdate) = {$year},paymentamount, 0)) AS currentyearpayed,
SUM(IF(YEAR(paymentdate) != {$year},paymentamount, 0)) AS otheryearpayed
FROM payments
GROUP BY invoice_id
) AS Q1
ON INV.id = Q1.invoice_id
LEFT JOIN (
SELECT project_id,
SUM( reservationamount) reserved
FROM reservations
GROUP BY project_id
) AS Q2
ON PRJ.id = Q2.project_id
LEFT JOIN (
SELECT project_id,
SUM(IF(year = {$year}, IF(month_id >= MONTH(NOW()) -1, forecastamount, 0), 0)) AS currentforecast,
SUM(IF(year > {$year}, forecastamount, 0)) AS otherforecast
FROM forecasts
GROUP BY project_id
) AS Q3
ON PRJ.id = Q3.project_id
GROUP BY PRJ.projectnumber, PRJ.projectname ";
return $sql;
}
}
@@ -0,0 +1,69 @@
<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 13/04/2018
* Time: 14:27
*/
class ReservationTotals {
public static function fire()
{
// UNION ALL wordt gebruikt voor een full outer join
$sql = "SELECT PRJ.id AS projectID, external_id AS externalID,
PRJ.projectname,
COALESCE(fromreservation, 0) AS fromreservation,
COALESCE(Q5.costs, 0) AS costs, COALESCE(reservation, 0) AS reservation,
COALESCE(payed, 0) AS payed, (COALESCE(reservation, 0) - COALESCE(fromreservation, 0)) AS saldo,
(COALESCE(Q5.costs, 0) - COALESCE(payed, 0)) AS saldoToPay, PRJ.projectnumber, EXT.contactperson,
ROUND(( (OFF.costs * (100 + (SELECT vatrate
FROM vats VTS
JOIN projects P
ON VTS.id = P.vat_id
WHERE P.id = PRJ.id LIMIT 1))) / 100 ), 0) other_revenue
FROM (
SELECT COALESCE(reservation, 0) AS reservation, costs, payed, Q2.external_id, Q2.project_id,
IF(from_reservation = 'T', payed, 0) fromreservation
FROM (
SELECT SUM(reservationamount) AS reservation, external_id, project_id
FROM reservations
GROUP BY external_id, project_id
) Q1
RIGHT JOIN
(
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, from_reservation
FROM projectcosts
GROUP BY external_id, project_id
) Q2
ON Q1.external_id = Q2.external_id AND Q1.project_id = Q2.project_id
UNION ALL
SELECT reservation, COALESCE(costs, 0), COALESCE(payed, 0), Q3.external_id, Q3.project_id,
IF(from_reservation = 'T', payed, 0) fromreservation
FROM (
SELECT SUM(reservationamount) AS reservation, external_id, project_id
FROM reservations
GROUP BY external_id, project_id
) Q3
LEFT JOIN
(
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, id, from_reservation
FROM projectcosts
GROUP BY external_id, project_id
) Q4
ON Q3.external_id = Q4.external_id AND Q3.project_id = Q4.project_id
WHERE Q4.id IS NULL
) Q5
RIGHT JOIN projects PRJ
ON PRJ.id = Q5.project_id
LEFT JOIN offers OFF
ON PRJ.id = OFF.project_id
LEFT JOIN externals EXT
ON EXT.id = Q5.external_id
WHERE OFF.costs > 0
AND PRJ.archive = 'F'";
return $sql;
}
}