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

170 lines
6.1 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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)
{
return DB::select(DB::raw('
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)
GROUP BY currency_id
'), ['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
}
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
{
return DB::select(DB::raw('
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
GROUP BY expenses.date
2022-01-21 04:35:16 +01:00
HAVING currency_id = :currency_id
'), [
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
{
return DB::select(DB::raw('
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
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
'), [
'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
]);
}
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
{
return DB::select(DB::raw('
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
WHERE payments.status_id IN (4,5,6)
AND (payments.date BETWEEN :start_date AND :end_date)
AND payments.company_id = :company_id
AND payments.is_deleted = 0
GROUP BY payments.date
2022-01-21 07:04:01 +01:00
HAVING currency_id = :currency_id
'), [
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
{
return DB::select(DB::raw("
2022-01-21 04:35:16 +01:00
SELECT
2022-05-20 12:05:23 +02:00
sum(invoices.balance) as amount,
IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :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 invoices.balance > 0
AND clients.is_deleted = 0
AND invoices.is_deleted = 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
"), ['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
}
2022-05-20 12:05:23 +02:00
public function getRevenueQuery($start_date, $end_date)
{
return DB::select(DB::raw("
2022-05-20 12:05:23 +02:00
SELECT
sum(invoices.paid_to_date) as paid_to_date,
IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id
FROM clients
JOIN invoices
on invoices.client_id = clients.id
WHERE invoices.status_id IN (3,4)
AND invoices.company_id = :company_id
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
"), ['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
2022-05-20 12:05:23 +02:00
public function getInvoiceChartQuery($start_date, $end_date, $currency_id)
{
return DB::select(DB::raw("
2022-05-20 12:05:23 +02:00
SELECT
sum(invoices.amount) as total,
invoices.date,
IFNULL(CAST(JSON_EXTRACT( 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.date BETWEEN :start_date AND :end_date)
AND invoices.company_id = :company_id
AND clients.is_deleted = 0
AND invoices.is_deleted = 0
GROUP BY invoices.date
HAVING currency_id = :currency_id
"), [
'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
]);
}
}