Fix for invoice details in project overview
This commit is contained in:
@@ -43,10 +43,29 @@ class ProjectsController extends BaseController
|
|||||||
$project->invoiceDetails = $builder
|
$project->invoiceDetails = $builder
|
||||||
->selectSum('invoiceamount', 'invoiceTotal')
|
->selectSum('invoiceamount', 'invoiceTotal')
|
||||||
->selectSum('forecastamount', 'forecastTotal')
|
->selectSum('forecastamount', 'forecastTotal')
|
||||||
|
// Join VAT to calculate totals including VAT per invoice
|
||||||
|
->join('vats', 'vats.id = invoices.vat_id', 'left')
|
||||||
|
// Total invoiced including VAT based on each invoice's VAT rate
|
||||||
|
->select('SUM(ROUND(invoices.invoiceamount * (100 + COALESCE(vats.vatrate, 0)) / 100)) AS totalinvoiced', false)
|
||||||
->where('project_id', $project->id)
|
->where('project_id', $project->id)
|
||||||
->get()
|
->get()
|
||||||
->getRow();
|
->getRow();
|
||||||
|
|
||||||
|
// Add total payments (VAT included amounts stored in payments table)
|
||||||
|
$paymentsBuilder = $db->table('payments');
|
||||||
|
$payments = $paymentsBuilder
|
||||||
|
->select('COALESCE(ROUND(SUM(payments.paymentamount), 0), 0) AS payed', false)
|
||||||
|
->join('invoices', 'invoices.id = payments.invoice_id', 'inner')
|
||||||
|
->where('invoices.project_id', $project->id)
|
||||||
|
->get()
|
||||||
|
->getRow();
|
||||||
|
|
||||||
|
// Ensure invoiceDetails exists and attach payed sum
|
||||||
|
if (!isset($project->invoiceDetails) || !$project->invoiceDetails) {
|
||||||
|
$project->invoiceDetails = (object) [];
|
||||||
|
}
|
||||||
|
$project->invoiceDetails->payed = $payments->payed ?? 0;
|
||||||
|
|
||||||
$project->hourTotals = \Projecthour::getTotalHoursPerProject($project->id);
|
$project->hourTotals = \Projecthour::getTotalHoursPerProject($project->id);
|
||||||
|
|
||||||
return $this->json($project);
|
return $this->json($project);
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ class QEmployeeHours {
|
|||||||
|
|
||||||
public static function fire( $month, $year )
|
public static function fire( $month, $year )
|
||||||
{
|
{
|
||||||
$sql = "
|
$sql = "
|
||||||
SELECT monthID,
|
SELECT MAX(monthID) AS monthID,
|
||||||
user_id,
|
user_id,
|
||||||
CONCAT(firstname, ' ', middlename, ' ', lastname) AS fullname,
|
MAX(CONCAT(firstname, ' ', middlename, ' ', lastname)) AS fullname,
|
||||||
ROUND(SUM(projecthours), 0) AS projecthours,
|
ROUND(SUM(projecthours), 0) AS projecthours,
|
||||||
ROUND(SUM(profitablehours), 0) AS profitablehours,
|
ROUND(SUM(profitablehours), 0) AS profitablehours,
|
||||||
ROUND(SUM(nonprofitablehours), 0) AS nonprofitablehours,
|
ROUND(SUM(nonprofitablehours), 0) AS nonprofitablehours,
|
||||||
ROUND(SUM(revenues), 0) AS revenues,
|
ROUND(SUM(revenues), 0) AS revenues,
|
||||||
ROUND(SUM(nonprojecthours), 0) AS nonprojecthours,
|
ROUND(SUM(nonprojecthours), 0) AS nonprojecthours,
|
||||||
ROUND(SUM(projecthours + nonprojecthours), 0) AS totalhours,
|
ROUND(SUM(projecthours + nonprojecthours), 0) AS totalhours,
|
||||||
targethours
|
MAX(targethours) AS targethours
|
||||||
FROM (
|
FROM (
|
||||||
SELECT employee_id AS user_id,
|
SELECT employee_id AS user_id,
|
||||||
MONTH(hourdate) monthID,
|
MONTH(hourdate) monthID,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class ReservationTotals {
|
|||||||
) Q1
|
) Q1
|
||||||
RIGHT JOIN
|
RIGHT JOIN
|
||||||
(
|
(
|
||||||
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, from_reservation
|
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, MAX(from_reservation) AS from_reservation
|
||||||
FROM projectcosts
|
FROM projectcosts
|
||||||
GROUP BY external_id, project_id
|
GROUP BY external_id, project_id
|
||||||
) Q2
|
) Q2
|
||||||
@@ -47,7 +47,7 @@ class ReservationTotals {
|
|||||||
) Q3
|
) Q3
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
(
|
(
|
||||||
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, id, from_reservation
|
SELECT SUM(costs) AS costs, SUM(payed) AS payed, external_id, project_id, MAX(id) AS id, MAX(from_reservation) AS from_reservation
|
||||||
FROM projectcosts
|
FROM projectcosts
|
||||||
GROUP BY external_id, project_id
|
GROUP BY external_id, project_id
|
||||||
) Q4
|
) Q4
|
||||||
|
|||||||
Reference in New Issue
Block a user