1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/Chart/ChartQueries.php

462 lines
17 KiB
PHP
Raw Normal View History

2022-01-20 10:09:08 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-01-20 10:09:08 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Chart;
use Illuminate\Support\Facades\DB;
/**
* Class ChartQueries.
*/
trait ChartQueries
{
2022-05-20 12:05:23 +02:00
/**
* Expenses
*/
2022-01-20 10:09:08 +01:00
public function getExpenseQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-01-20 10:09:08 +01:00
SELECT sum(expenses.amount) as amount,
2022-01-21 02:25:13 +01:00
IFNULL(expenses.currency_id, :company_currency) as currency_id
2022-01-20 10:09:08 +01:00
FROM expenses
WHERE expenses.is_deleted = 0
AND expenses.company_id = :company_id
AND (expenses.date BETWEEN :start_date AND :end_date)
{$user_filter}
2022-01-20 10:09:08 +01:00
GROUP BY currency_id
2023-09-04 02:27:28 +02:00
", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
2022-01-20 10:09:08 +01:00
}
2024-06-24 13:10:52 +02:00
public function getAggregateExpenseQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-06-25 06:58:18 +02:00
SUM(
CASE
WHEN expenses.currency_id = :company_currency THEN amount
2024-10-05 06:06:43 +02:00
ELSE expenses.amount * COALESCE(NULLIF(expenses.exchange_rate, 0), 1)
2024-06-25 06:58:18 +02:00
END
) AS amount
2024-06-24 13:10:52 +02:00
FROM expenses
WHERE expenses.is_deleted = 0
AND expenses.company_id = :company_id
AND (expenses.date BETWEEN :start_date AND :end_date)
{$user_filter}
2024-06-25 06:58:18 +02:00
", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
2024-06-24 13:10:52 +02:00
}
public function getAggregateExpenseChartQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-06-25 06:58:18 +02:00
SUM(
CASE
WHEN expenses.currency_id = :company_currency THEN amount
2024-10-05 06:06:43 +02:00
ELSE expenses.amount * COALESCE(NULLIF(expenses.exchange_rate, 0), 1)
2024-06-25 06:58:18 +02:00
END
2024-10-05 02:20:18 +02:00
) AS total,
2024-06-24 13:10:52 +02:00
expenses.date
FROM expenses
WHERE (expenses.date BETWEEN :start_date AND :end_date)
AND expenses.company_id = :company_id
AND expenses.is_deleted = 0
{$user_filter}
GROUP BY expenses.date
", [
'company_currency' => $this->company->settings->currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
2022-05-20 12:05:23 +02:00
public function getExpenseChartQuery($start_date, $end_date, $currency_id)
2022-01-21 04:35:16 +01:00
{
$user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-01-21 04:35:16 +01:00
SELECT
2022-05-20 12:05:23 +02:00
sum(expenses.amount) as total,
expenses.date,
IFNULL(expenses.currency_id, :company_currency) AS currency_id
FROM expenses
WHERE (expenses.date BETWEEN :start_date AND :end_date)
AND expenses.company_id = :company_id
AND expenses.is_deleted = 0
{$user_filter}
2022-05-20 12:05:23 +02:00
GROUP BY expenses.date
2022-01-21 04:35:16 +01:00
HAVING currency_id = :currency_id
2023-09-04 02:27:28 +02:00
", [
2022-05-20 12:05:23 +02:00
'company_currency' => $this->company->settings->currency_id,
2022-01-21 04:35:16 +01:00
'currency_id' => $currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
2022-01-21 04:35:16 +01:00
]);
2022-01-21 07:04:01 +01:00
}
2022-01-21 04:35:16 +01:00
2022-05-20 12:05:23 +02:00
/**
* Payments
*/
public function getPaymentQuery($start_date, $end_date)
2022-01-21 07:04:01 +01:00
{
2024-01-14 05:05:00 +01:00
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-05-20 12:05:23 +02:00
SELECT sum(payments.amount) as amount,
IFNULL(payments.currency_id, :company_currency) as currency_id
2022-01-21 07:04:01 +01:00
FROM payments
2022-05-20 12:05:23 +02:00
WHERE payments.is_deleted = 0
{$user_filter}
2022-01-21 07:04:01 +01:00
AND payments.company_id = :company_id
2022-05-20 12:05:23 +02:00
AND (payments.date BETWEEN :start_date AND :end_date)
GROUP BY currency_id
2023-09-04 02:27:28 +02:00
", [
'company_currency' => $this->company->settings->currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
2022-01-21 07:04:01 +01:00
]);
}
2024-06-24 13:10:52 +02:00
public function getAggregatePaymentQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
return DB::select("
2024-10-05 06:06:43 +02:00
SELECT sum((payments.amount - payments.refunded) / COALESCE(NULLIF(payments.exchange_rate, 0), 1)) as amount,
2024-06-24 13:10:52 +02:00
IFNULL(payments.currency_id, :company_currency) as currency_id
FROM payments
2024-10-05 02:20:18 +02:00
WHERE payments.company_id = :company_id
AND payments.is_deleted = 0
2024-06-24 13:10:52 +02:00
{$user_filter}
2024-10-05 02:20:18 +02:00
AND payments.status_id IN (4,5,6)
2024-06-24 13:10:52 +02:00
AND (payments.date BETWEEN :start_date AND :end_date)
GROUP BY currency_id
", [
'company_currency' => $this->company->settings->currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
public function getAggregatePaymentChartQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-10-05 06:06:43 +02:00
sum((payments.amount - payments.refunded) * COALESCE(NULLIF(payments.exchange_rate, 0), 1)) as total,
2024-10-05 02:20:18 +02:00
payments.date
2024-06-24 13:10:52 +02:00
FROM payments
WHERE payments.company_id = :company_id
AND payments.is_deleted = 0
{$user_filter}
AND payments.status_id IN (4,5,6)
AND (payments.date BETWEEN :start_date AND :end_date)
GROUP BY payments.date
", [
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
2022-05-20 12:05:23 +02:00
public function getPaymentChartQuery($start_date, $end_date, $currency_id)
2022-01-21 07:04:01 +01:00
{
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-01-21 07:04:01 +01:00
SELECT
2022-05-20 12:05:23 +02:00
sum(payments.amount - payments.refunded) as total,
payments.date,
IFNULL(payments.currency_id, :company_currency) AS currency_id
FROM payments
2023-05-12 03:19:10 +02:00
WHERE payments.company_id = :company_id
2022-05-20 12:05:23 +02:00
AND payments.is_deleted = 0
{$user_filter}
2023-05-12 03:19:10 +02:00
AND payments.status_id IN (4,5,6)
2023-05-12 02:52:06 +02:00
AND (payments.date BETWEEN :start_date AND :end_date)
2022-05-20 12:05:23 +02:00
GROUP BY payments.date
2022-01-21 07:04:01 +01:00
HAVING currency_id = :currency_id
2023-09-04 02:27:28 +02:00
", [
2022-01-21 07:04:01 +01:00
'company_currency' => $this->company->settings->currency_id,
'currency_id' => $currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
2022-01-21 07:04:01 +01:00
]);
}
2022-01-21 04:35:16 +01:00
/**
2022-05-20 12:05:23 +02:00
* Invoices
*/
public function getOutstandingQuery($start_date, $end_date)
2022-01-21 04:35:16 +01:00
{
2024-01-14 05:05:00 +01:00
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-01-21 04:35:16 +01:00
SELECT
2022-05-20 12:05:23 +02:00
sum(invoices.balance) as amount,
2023-05-14 01:25:08 +02:00
COUNT(*) as outstanding_count,
2023-05-12 02:52:06 +02:00
IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id
2022-01-21 04:35:16 +01:00
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.status_id IN (2,3)
AND invoices.company_id = :company_id
AND clients.is_deleted = 0
{$user_filter}
2022-01-21 04:35:16 +01:00
AND invoices.is_deleted = 0
2023-05-12 03:19:10 +02:00
AND invoices.balance > 0
2022-05-20 12:05:23 +02:00
AND (invoices.date BETWEEN :start_date AND :end_date)
2022-01-21 04:35:16 +01:00
GROUP BY currency_id
2023-09-04 02:27:28 +02:00
", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
2022-01-21 04:35:16 +01:00
}
2024-06-24 13:10:52 +02:00
public function getAggregateOutstandingQuery($start_date, $end_date)
2022-05-20 12:05:23 +02:00
{
2024-08-22 08:45:06 +02:00
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-05-20 12:05:23 +02:00
SELECT
2024-10-05 06:06:43 +02:00
SUM(invoices.balance / COALESCE(NULLIF(invoices.exchange_rate, 0), 1)) as amount,
2024-10-05 02:20:18 +02:00
COUNT(invoices.id) as outstanding_count
2022-05-20 12:05:23 +02:00
FROM clients
JOIN invoices
on invoices.client_id = clients.id
2024-06-24 13:10:52 +02:00
WHERE invoices.status_id IN (2,3)
AND invoices.company_id = :company_id
2023-05-12 03:19:10 +02:00
AND clients.is_deleted = 0
{$user_filter}
2023-05-12 03:19:10 +02:00
AND invoices.is_deleted = 0
2024-06-24 13:10:52 +02:00
AND invoices.balance > 0
2023-05-12 03:19:10 +02:00
AND (invoices.date BETWEEN :start_date AND :end_date)
2024-06-24 13:10:52 +02:00
", [
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date]);
2023-05-12 03:19:10 +02:00
}
2024-06-24 13:10:52 +02:00
public function getAggregateRevenueQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-10-05 06:06:43 +02:00
sum((payments.amount - payments.refunded) * COALESCE(NULLIF(payments.exchange_rate, 0), 1)) as paid_to_date
2024-06-24 13:10:52 +02:00
FROM payments
2024-06-25 06:58:18 +02:00
JOIN clients
ON payments.client_id=clients.id
2024-06-24 13:10:52 +02:00
WHERE payments.company_id = :company_id
AND payments.is_deleted = 0
2024-06-25 06:58:18 +02:00
AND clients.is_deleted = 0
2024-06-24 13:10:52 +02:00
{$user_filter}
AND payments.status_id IN (1,4,5,6)
AND (payments.date BETWEEN :start_date AND :end_date)
2024-06-25 06:58:18 +02:00
GROUP BY payments.company_id
2024-06-24 13:10:52 +02:00
", ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
}
2023-08-02 11:47:26 +02:00
public function getRevenueQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id;
2023-09-04 02:34:14 +02:00
return DB::select("
2023-08-02 11:47:26 +02:00
SELECT
sum(payments.amount - payments.refunded) as paid_to_date,
payments.currency_id AS currency_id
FROM payments
2024-06-25 06:58:18 +02:00
JOIN clients
ON payments.client_id=clients.id
2023-08-02 11:47:26 +02:00
WHERE payments.company_id = :company_id
AND payments.is_deleted = 0
2024-06-25 06:58:18 +02:00
AND clients.is_deleted = 0
2023-08-02 11:47:26 +02:00
{$user_filter}
AND payments.status_id IN (1,4,5,6)
AND (payments.date BETWEEN :start_date AND :end_date)
GROUP BY payments.currency_id
2023-09-04 02:27:28 +02:00
", ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
2023-08-02 11:47:26 +02:00
}
2023-08-02 11:10:39 +02:00
2024-06-24 13:10:52 +02:00
public function getAggregateInvoicesQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-10-05 06:06:43 +02:00
SUM(invoices.amount / COALESCE(NULLIF(invoices.exchange_rate, 0), 1)) as invoiced_amount
2024-06-24 13:10:52 +02:00
FROM clients
2024-10-05 02:20:18 +02:00
JOIN invoices ON invoices.client_id = clients.id
2024-06-24 13:10:52 +02:00
WHERE invoices.status_id IN (2,3,4)
AND invoices.company_id = :company_id
{$user_filter}
AND invoices.amount > 0
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
AND (invoices.date BETWEEN :start_date AND :end_date)
2024-10-05 02:20:18 +02:00
", [
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date
]);
2024-06-24 13:10:52 +02:00
}
2023-05-12 03:19:10 +02:00
public function getInvoicesQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2023-05-12 03:19:10 +02:00
SELECT
sum(invoices.amount) as invoiced_amount,
IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.status_id IN (2,3,4)
2022-05-20 12:05:23 +02:00
AND invoices.company_id = :company_id
{$user_filter}
2022-05-20 12:05:23 +02:00
AND invoices.amount > 0
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
AND (invoices.date BETWEEN :start_date AND :end_date)
GROUP BY currency_id
2023-09-04 02:27:28 +02:00
", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
2022-05-20 12:05:23 +02:00
}
2022-01-21 04:35:16 +01:00
2024-06-24 13:10:52 +02:00
public function getAggregateOutstandingChartQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-10-05 06:06:43 +02:00
SUM(invoices.balance / COALESCE(NULLIF(invoices.exchange_rate, 0), 1)) as total,
2024-06-24 13:10:52 +02:00
invoices.date
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.status_id IN (2,3,4)
AND invoices.company_id = :company_id
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
{$user_filter}
AND (invoices.date BETWEEN :start_date AND :end_date)
", [
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
2023-05-12 02:52:06 +02:00
public function getOutstandingChartQuery($start_date, $end_date, $currency_id)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2023-05-12 02:52:06 +02:00
SELECT
sum(invoices.balance) as total,
invoices.date,
IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.status_id IN (2,3,4)
AND invoices.company_id = :company_id
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
{$user_filter}
2023-05-12 02:52:06 +02:00
AND (invoices.date BETWEEN :start_date AND :end_date)
GROUP BY invoices.date
HAVING currency_id = :currency_id
2023-09-04 02:27:28 +02:00
", [
2023-05-12 02:52:06 +02:00
'company_currency' => (int) $this->company->settings->currency_id,
'currency_id' => $currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
2024-06-24 13:10:52 +02:00
public function getAggregateInvoiceChartQuery($start_date, $end_date)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
return DB::select("
SELECT
2024-10-05 06:06:43 +02:00
SUM(invoices.amount / COALESCE(NULLIF(invoices.exchange_rate, 0), 1)) as total,
2024-06-24 13:10:52 +02:00
invoices.date
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.company_id = :company_id
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
{$user_filter}
AND invoices.status_id IN (2,3,4)
AND (invoices.date BETWEEN :start_date AND :end_date)
", [
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
]);
}
2022-05-20 12:05:23 +02:00
public function getInvoiceChartQuery($start_date, $end_date, $currency_id)
{
$user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id;
2023-09-04 02:27:28 +02:00
return DB::select("
2022-05-20 12:05:23 +02:00
SELECT
sum(invoices.amount) as total,
invoices.date,
2023-05-12 02:52:06 +02:00
IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id
2022-05-20 12:05:23 +02:00
FROM clients
JOIN invoices
on invoices.client_id = clients.id
2023-05-12 03:19:10 +02:00
WHERE invoices.company_id = :company_id
2022-05-20 12:05:23 +02:00
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
{$user_filter}
2023-05-12 03:19:10 +02:00
AND invoices.status_id IN (2,3,4)
2023-05-12 02:52:06 +02:00
AND (invoices.date BETWEEN :start_date AND :end_date)
2022-05-20 12:05:23 +02:00
GROUP BY invoices.date
HAVING currency_id = :currency_id
2023-09-04 02:27:28 +02:00
", [
'company_currency' => (int) $this->company->settings->currency_id,
2022-05-20 12:05:23 +02:00
'currency_id' => $currency_id,
'company_id' => $this->company->id,
'start_date' => $start_date,
'end_date' => $end_date,
2022-05-20 12:05:23 +02:00
]);
}
}