Invoice report

This commit is contained in:
2018-03-07 17:03:45 +01:00
parent f540bab261
commit ad478b02b1
25 changed files with 1006 additions and 125 deletions
+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();
}
}
+12
View File
@@ -31,6 +31,16 @@ class Project extends BaseModel {
return $this->hasMany( 'Invoice' );
}
public function forecasts()
{
return $this->hasMany( 'Forecast' );
}
public function reservations()
{
return $this->hasMany( 'Reservation' );
}
public function projectleader()
{
return $this->belongsTo( 'User', 'projectleader_id' );
@@ -96,6 +106,8 @@ class Project extends BaseModel {
'projectemployees.user' => function ( $q ) {
$q->selectRaw( "id, CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname" );
}] )
->with(['forecasts'])
->with(['reservations'])
->with( 'projectemployees.projecthours' )
->with( 'projectcosts.external' )
->with( 'invoices.payments' )
@@ -0,0 +1,38 @@
<?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 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();
}
}
@@ -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
JOIN projects PRJ
GROUP BY MNT.id
ORDER BY MNT.id ";
return $sql;
}
}
@@ -16,11 +16,16 @@ class Qinvoiceoverview {
PRJ.projectname,
(own_revenue + other_revenue ) AS revenues,
CUS.name AS customername,
IF( YEAR(invoicedate) = {$year},SUM(invoiceamount), 0) AS currentyear,
IF( YEAR(invoicedate) != {$year},SUM(invoiceamount), 0) AS otheryear,
COALESCE(Q1.currentyearpayed, 0) AS currentyearpayed,
COALESCE(Q1.otheryearpayed, 0) AS otheryearpayed,
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname
SUM(IF( YEAR(invoicedate) = {$year},invoiceamount, 0)) AS currentyear,
SUM(IF( YEAR(invoicedate) != {$year},invoiceamount, 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(reservedpayed, 0 ) AS reservedpayed,
COALESCE(currentforecast, 0) AS currentforecast,
COALESCE(otherforecast, 0) AS otherforecast
FROM invoices INV
RIGHT JOIN projects PRJ
ON PRJ.id = INV.project_id
@@ -38,6 +43,22 @@ class Qinvoiceoverview {
GROUP BY invoice_id
) AS Q1
ON INV.id = Q1.invoice_id
LEFT JOIN (
SELECT project_id,
SUM(IF(payed='UNPAYED', reservationamount, 0)) reserved,
SUM(IF(payed='PAYED', reservationamount, 0)) reservedpayed
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;