Fix for invoice details in project overview
This commit is contained in:
@@ -43,10 +43,29 @@ class ProjectsController extends BaseController
|
||||
$project->invoiceDetails = $builder
|
||||
->selectSum('invoiceamount', 'invoiceTotal')
|
||||
->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)
|
||||
->get()
|
||||
->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);
|
||||
|
||||
return $this->json($project);
|
||||
|
||||
Reference in New Issue
Block a user