1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Report/ProfitLoss.php

648 lines
22 KiB
PHP
Raw Normal View History

<?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)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Report;
2023-10-26 04:57:44 +02:00
use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Libraries\MultiDB;
use App\Models\Company;
2023-10-26 04:57:44 +02:00
use App\Models\Currency;
2022-05-11 08:29:56 +02:00
use App\Models\Expense;
2023-06-18 05:35:51 +02:00
use App\Models\Invoice;
2022-05-13 03:13:25 +02:00
use App\Models\Payment;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Number;
use Illuminate\Support\Carbon;
2022-05-13 08:42:04 +02:00
use Illuminate\Support\Facades\App;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Str;
use League\Csv\Writer;
2024-02-24 03:28:19 +01:00
use function Sentry\continueTrace;
class ProfitLoss
{
private bool $is_income_billed = true;
private bool $is_expense_billed = true;
private bool $is_tax_included = true;
private $start_date;
private $end_date;
private float $income = 0;
private float $income_taxes = 0;
2022-05-13 03:13:25 +02:00
private float $credit = 0;
2022-05-13 05:07:42 +02:00
private float $credit_invoice = 0;
2022-05-13 03:13:25 +02:00
private float $credit_taxes = 0;
2022-05-13 05:07:42 +02:00
private array $invoice_payment_map = [];
private array $expenses = [];
private array $expense_break_down = [];
private array $income_map;
2022-05-13 09:15:05 +02:00
private array $foreign_income = [];
2022-05-11 08:29:56 +02:00
protected CurrencyApi $currency_api;
/*
payload variables.
start_date - Y-m-d
end_date - Y-m-d
date_range -
all
last7
last30
this_month
last_month
this_quarter
last_quarter
this_year
custom
is_income_billed - true = Invoiced || false = Payments
is_expense_billed - true = Expensed || false = Expenses marked as paid
include_tax - true tax_included || false - tax_excluded
*/
protected array $payload;
protected Company $company;
2022-05-12 02:57:58 +02:00
public function __construct(Company $company, array $payload)
{
2022-05-12 02:57:58 +02:00
$this->currency_api = new CurrencyApi();
$this->company = $company;
$this->payload = $payload;
$this->setBillingReportType();
}
2023-01-28 04:59:35 +01:00
public function run()
{
return $this->build()->getCsv();
}
public function build()
{
MultiDB::setDb($this->company->db);
if ($this->is_income_billed) { //get invoiced amounts
$this->filterIncome();
} else {
2022-05-13 01:11:40 +02:00
$this->filterInvoicePaymentIncome();
}
2022-05-13 05:07:42 +02:00
$this->expenseData()->buildExpenseBreakDown();
return $this;
}
2024-01-14 05:05:00 +01:00
public function getIncome(): float
{
return round($this->income, 2);
}
2024-01-14 05:05:00 +01:00
public function getIncomeMap(): array
{
return $this->income_map;
}
2024-01-14 05:05:00 +01:00
public function getIncomeTaxes(): float
{
return round($this->income_taxes, 2);
}
2024-01-14 05:05:00 +01:00
public function getExpenses(): array
{
return $this->expenses;
}
2024-01-14 05:05:00 +01:00
public function getExpenseBreakDown(): array
{
2022-05-13 08:42:04 +02:00
ksort($this->expense_break_down);
2022-05-13 05:07:42 +02:00
return $this->expense_break_down;
}
private function filterIncome()
{
$invoices = $this->invoiceIncome();
2022-05-13 09:15:05 +02:00
$this->foreign_income = [];
$this->income = 0;
$this->income_taxes = 0;
$this->income_map = $invoices;
foreach ($invoices as $invoice) {
$this->income += $invoice->net_converted_amount;
$this->income_taxes += $invoice->net_converted_taxes;
2022-05-13 09:15:05 +02:00
$currency = Currency::find(intval(str_replace('"', '', $invoice->currency_id)));
2022-05-13 09:15:05 +02:00
$currency->name = ctrans('texts.currency_'.Str::slug($currency->name, '_'));
$this->foreign_income[] = ['currency' => $currency->name, 'amount' => $invoice->amount, 'total_taxes' => $invoice->total_taxes];
}
return $this;
}
2022-05-12 23:52:02 +02:00
private function filterInvoicePaymentIncome()
{
2022-05-13 05:07:42 +02:00
$this->paymentEloquentIncome();
2022-05-12 23:52:02 +02:00
foreach ($this->invoice_payment_map as $map) {
2022-05-13 05:07:42 +02:00
$this->income += $map->amount_payment_paid_converted - $map->tax_amount_converted;
$this->income_taxes += $map->tax_amount_converted;
2022-05-12 23:52:02 +02:00
2022-05-13 05:07:42 +02:00
$this->credit += $map->amount_credit_paid_converted - $map->tax_amount_credit_converted;
$this->credit_taxes += $map->tax_amount_credit_converted;
2022-05-12 23:52:02 +02:00
}
return $this;
}
2024-01-14 05:05:00 +01:00
private function getForeignIncome(): array
2022-05-13 09:15:05 +02:00
{
return $this->foreign_income;
}
private function filterPaymentIncome()
{
$payments = $this->paymentIncome();
return $this;
}
/*
//returns an array of objects
=> [
{#2047
+"amount": "706.480000",
+"total_taxes": "35.950000",
+"currency_id": ""1"",
+"net_converted_amount": "670.5300000000",
+"net_converted_taxes": "10"
},
{#2444
+"amount": "200.000000",
+"total_taxes": "0.000000",
+"currency_id": ""23"",
+"net_converted_amount": "1.7129479802",
+"net_converted_taxes": "10"
},
{#2654
+"amount": "140.000000",
+"total_taxes": "40.000000",
+"currency_id": ""12"",
+"net_converted_amount": "69.3275024282",
+"net_converted_taxes": "10"
},
]
*/
private function invoiceIncome()
{
2023-09-04 02:27:28 +02:00
return \DB::select("
SELECT
sum(invoices.amount) as amount,
sum(invoices.total_taxes) as total_taxes,
(sum(invoices.total_taxes) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_taxes,
sum(invoices.amount - invoices.total_taxes) as net_amount,
2023-05-12 03:36:07 +02:00
IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id,
(sum(invoices.amount - invoices.total_taxes) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_amount
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 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' => $this->start_date, 'end_date' => $this->end_date]);
}
2022-05-13 09:15:05 +02:00
/**
* The income calculation is based on the total payments received during
* the selected time period.
*
2022-05-13 09:15:05 +02:00
* Once we have the payments we iterate through the attached invoices and
* we also determine the total taxes paid as our
* Profit and loss statement should be net of all taxes
*
2022-05-13 09:15:05 +02:00
* This calculation also considers partial payments and pro rata's any taxes.
*
2022-05-13 09:15:05 +02:00
* This calculation also considers exchange rates and we convert (based on the payment exchange rate)
* to the native company currency.
*/
2022-05-13 03:13:25 +02:00
private function paymentEloquentIncome()
{
2022-05-13 05:07:42 +02:00
$this->invoice_payment_map = [];
2022-05-13 03:13:25 +02:00
2023-08-06 09:35:19 +02:00
Payment::query()->where('company_id', $this->company->id)
->whereIn('status_id', [1, 4, 5])
2022-05-13 03:13:25 +02:00
->where('is_deleted', 0)
->whereBetween('date', [$this->start_date, $this->end_date])
->whereHas('client', function ($query) {
$query->where('is_deleted', 0);
2022-05-13 03:13:25 +02:00
})
->with(['company', 'client'])
2022-05-13 03:13:25 +02:00
->cursor()
->each(function ($payment) {
2022-05-13 05:07:42 +02:00
2024-01-14 05:05:00 +01:00
$map = new \stdClass();
2022-05-13 05:07:42 +02:00
$amount_payment_paid = 0;
$amount_credit_paid = 0;
$amount_payment_paid_converted = 0;
$amount_credit_paid_converted = 0;
$tax_amount = 0;
$tax_amount_converted = 0;
$tax_amount_credit = 0;
$tax_amount_credit_converted = $tax_amount_credit_converted = 0;
2024-02-24 03:28:19 +01:00
$invoice = false;
foreach ($payment->paymentables as $pivot) {
2023-06-18 05:35:51 +02:00
if ($pivot->paymentable_type == 'invoices') {
2023-08-08 11:44:52 +02:00
$invoice = Invoice::query()->withTrashed()->find($pivot->paymentable_id);
2022-05-13 05:07:42 +02:00
2024-06-14 09:09:44 +02:00
if(!$invoice) {
2024-02-24 03:28:19 +01:00
continue;
2024-06-14 09:09:44 +02:00
}
2024-02-24 03:28:19 +01:00
2023-12-29 22:27:42 +01:00
$pivot_diff = $pivot->amount - $pivot->refunded;
$amount_payment_paid += $pivot_diff;
2024-02-20 11:16:00 +01:00
$amount_payment_paid_converted += $pivot_diff * ($payment->exchange_rate ?: 1);
2024-01-14 05:05:00 +01:00
2023-06-19 14:13:34 +02:00
if ($invoice->amount > 0) {
2023-12-29 22:27:42 +01:00
$tax_amount += ($pivot_diff / $invoice->amount) * $invoice->total_taxes;
2024-02-20 11:16:00 +01:00
$tax_amount_converted += (($pivot_diff / $invoice->amount) * $invoice->total_taxes) / $invoice->exchange_rate;
2023-06-19 14:13:34 +02:00
}
2023-06-19 14:40:52 +02:00
2022-05-13 03:13:25 +02:00
}
2024-06-14 09:09:44 +02:00
if(!$invoice) {
continue;
}
2022-05-13 03:13:25 +02:00
2023-06-18 05:35:51 +02:00
if ($pivot->paymentable_type == 'credits') {
2022-05-13 03:13:25 +02:00
$amount_credit_paid += $pivot->amount - $pivot->refunded;
2024-02-20 11:16:00 +01:00
$amount_credit_paid_converted += $pivot_diff * ($payment->exchange_rate ?: 1);
2022-05-13 03:13:25 +02:00
2023-12-29 22:27:42 +01:00
$tax_amount_credit += ($pivot_diff / $invoice->amount) * $invoice->total_taxes;
2024-02-20 11:16:00 +01:00
$tax_amount_credit_converted += (($pivot_diff / $invoice->amount) * $invoice->total_taxes) / $invoice->exchange_rate;
2022-05-13 03:13:25 +02:00
}
}
2022-05-13 05:07:42 +02:00
$map->amount_payment_paid = $amount_payment_paid;
$map->amount_payment_paid_converted = $amount_payment_paid_converted;
$map->tax_amount = $tax_amount;
$map->tax_amount_converted = $tax_amount_converted;
$map->amount_credit_paid = $amount_credit_paid;
$map->amount_credit_paid_converted = $amount_credit_paid_converted;
$map->tax_amount_credit = $tax_amount_credit;
$map->tax_amount_credit_converted = $tax_amount_credit_converted;
$map->currency_id = $payment->currency_id;
$this->invoice_payment_map[] = $map;
2022-05-13 03:13:25 +02:00
});
2022-05-13 05:07:42 +02:00
return $this;
}
2022-05-12 23:52:02 +02:00
/**
=> [
{#2047
+"amount": "110.000000",
+"total_taxes": "10.0000000000000000",
+"net_converted_amount": "110.0000000000",
+"net_converted_taxes": "10.00000000000000000000",
+"currency_id": ""1"",
},
{#2444
+"amount": "50.000000",
+"total_taxes": "4.5454545454545455",
+"net_converted_amount": "61.1682150381",
+"net_converted_taxes": "5.56074682164393914741",
+"currency_id": ""2"",
},
]
*/
2022-05-13 08:42:04 +02:00
public function getCsv()
{
2024-02-21 03:58:08 +01:00
nlog($this->income);
nlog($this->income_taxes);
nlog(array_sum(array_column($this->expense_break_down, 'total')));
2022-05-13 08:42:04 +02:00
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
App::setLocale($this->company->locale());
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
$csv = Writer::createFromString();
$csv->insertOne([ctrans('texts.profit_and_loss')]);
$csv->insertOne([ctrans('texts.company_name'), $this->company->present()->name()]);
$csv->insertOne([ctrans('texts.date_range'), Carbon::parse($this->start_date)->format($this->company->date_format()), Carbon::parse($this->end_date)->format($this->company->date_format())]);
//gross sales ex tax
$csv->insertOne(['--------------------']);
2024-02-21 03:58:08 +01:00
$csv->insertOne([ctrans('texts.total_revenue'). "[".ctrans('texts.tax')." " .ctrans('texts.exclusive'). "]", Number::formatMoney($this->income, $this->company)]);
2022-05-13 08:42:04 +02:00
//total taxes
$csv->insertOne([ctrans('texts.total_taxes'), Number::formatMoney($this->income_taxes, $this->company)]);
//expense
$csv->insertOne(['--------------------']);
foreach ($this->expense_break_down as $expense_breakdown) {
2022-05-13 08:42:04 +02:00
$csv->insertOne([$expense_breakdown['category_name'], Number::formatMoney($expense_breakdown['total'], $this->company)]);
}
//total expense taxes
$csv->insertOne(['--------------------']);
2024-02-21 03:58:08 +01:00
$csv->insertOne([ctrans('texts.total_expenses'). "[".ctrans('texts.tax')." " .ctrans('texts.exclusive'). "]", Number::formatMoney(array_sum(array_column($this->expense_break_down, 'total')), $this->company)]);
2022-05-13 08:42:04 +02:00
$csv->insertOne([ctrans('texts.total_taxes'), Number::formatMoney(array_sum(array_column($this->expense_break_down, 'tax')), $this->company)]);
$csv->insertOne(['--------------------']);
2024-02-21 03:58:08 +01:00
$csv->insertOne([ctrans('texts.total_profit'), Number::formatMoney($this->income - array_sum(array_column($this->expense_break_down, 'total')), $this->company)]);
2022-05-13 08:42:04 +02:00
//net profit
2022-05-13 09:15:05 +02:00
$csv->insertOne(['--------------------']);
$csv->insertOne(['']);
$csv->insertOne(['']);
2024-06-14 09:09:44 +02:00
2024-02-21 03:58:08 +01:00
$csv->insertOne(['--------------------']);
$csv->insertOne([ctrans('texts.revenue')]);
$csv->insertOne(['--------------------']);
2022-05-13 09:15:05 +02:00
$csv->insertOne([ctrans('texts.currency'), ctrans('texts.amount'), ctrans('texts.total_taxes')]);
foreach ($this->foreign_income as $foreign_income) {
2022-05-13 09:15:05 +02:00
$csv->insertOne([$foreign_income['currency'], ($foreign_income['amount'] - $foreign_income['total_taxes']), $foreign_income['total_taxes']]);
}
2024-02-21 03:58:08 +01:00
$csv->insertOne(['']);
$csv->insertOne(['']);
$csv->insertOne(['--------------------']);
$csv->insertOne([ctrans('texts.expenses')]);
$csv->insertOne(['--------------------']);
2024-06-14 09:09:44 +02:00
foreach($this->expenses as $expense) {
2024-02-21 03:58:08 +01:00
$csv->insertOne([$expense->currency, ($expense->total - $expense->foreign_tax_amount), $expense->foreign_tax_amount]);
}
return $csv->toString();
2022-05-13 08:42:04 +02:00
}
2022-05-13 05:07:42 +02:00
/**
+"payments": "12260.870000",
+"payments_converted": "12260.870000000000",
+"currency_id": 1,
*/
private function paymentIncome()
{
2023-09-04 02:27:28 +02:00
return \DB::select('
SELECT
SUM(coalesce(payments.amount - payments.refunded,0)) as payments,
SUM(coalesce(payments.amount - payments.refunded,0)) * IFNULL(payments.exchange_rate ,1) as payments_converted,
payments.currency_id as currency_id
FROM clients
INNER JOIN
payments ON
clients.id=payments.client_id
WHERE payments.status_id IN (1,4,5,6)
AND clients.is_deleted = false
AND payments.is_deleted = false
AND payments.company_id = :company_id
AND (payments.date BETWEEN :start_date AND :end_date)
GROUP BY currency_id
ORDER BY currency_id;
2023-09-04 02:27:28 +02:00
', ['company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]);
}
private function expenseData()
2022-05-11 08:29:56 +02:00
{
2023-08-06 09:35:19 +02:00
$expenses = Expense::query()->where('company_id', $this->company->id)
2024-06-14 09:09:44 +02:00
->where(function ($query) {
$query->whereNull('client_id')->orWhereHas('client', function ($q) {
$q->where('is_deleted', 0);
});
2024-02-20 11:16:00 +01:00
})
2022-05-11 08:29:56 +02:00
->where('is_deleted', 0)
->withTrashed()
->whereBetween('date', [$this->start_date, $this->end_date])
->cursor();
2022-05-13 05:07:42 +02:00
$this->expenses = [];
2024-02-21 03:58:08 +01:00
$company_currency_code = $this->company->currency()->code;
foreach ($expenses as $expense) {
2024-01-14 05:05:00 +01:00
$map = new \stdClass();
2022-05-13 05:07:42 +02:00
2024-02-21 03:58:08 +01:00
$expense_tax_total = $this->getTax($expense);
2022-05-13 05:07:42 +02:00
$map->total = $expense->amount;
2024-02-21 03:58:08 +01:00
$map->converted_total = $converted_total = $this->getConvertedTotal($expense->amount, $expense->exchange_rate); //converted to company currency
$map->tax = $tax = $this->getConvertedTotal($expense_tax_total, $expense->exchange_rate); //tax component
$map->net_converted_total = $expense->uses_inclusive_taxes ? ($converted_total - $tax) : $converted_total; //excludes all taxes
2022-05-13 05:07:42 +02:00
$map->category_id = $expense->category_id;
$map->category_name = $expense->category ? $expense->category->name : 'No Category Defined';
2022-05-13 05:07:42 +02:00
$map->currency_id = $expense->currency_id ?: $expense->company->settings->currency_id;
2024-02-21 03:58:08 +01:00
$map->currency = $expense->currency ? $expense->currency->code : $company_currency_code;
$map->foreign_tax_amount = $expense_tax_total;
$this->expenses[] = $map;
2022-05-13 05:07:42 +02:00
}
return $this;
2022-05-11 08:29:56 +02:00
}
2022-05-13 05:07:42 +02:00
private function buildExpenseBreakDown()
2022-05-11 08:29:56 +02:00
{
2022-05-11 14:38:19 +02:00
$data = [];
2022-05-11 08:29:56 +02:00
foreach ($this->expenses as $expense) {
if (! array_key_exists($expense->category_id, $data)) {
2022-05-13 05:07:42 +02:00
$data[$expense->category_id] = [];
}
2022-05-13 05:07:42 +02:00
if (! array_key_exists('total', $data[$expense->category_id])) {
2022-05-13 05:07:42 +02:00
$data[$expense->category_id]['total'] = 0;
}
if (! array_key_exists('tax', $data[$expense->category_id])) {
2022-05-13 05:07:42 +02:00
$data[$expense->category_id]['tax'] = 0;
}
2022-05-13 06:06:21 +02:00
$data[$expense->category_id]['total'] += $expense->net_converted_total;
2022-05-13 05:07:42 +02:00
$data[$expense->category_id]['category_name'] = $expense->category_name;
2022-05-13 06:06:21 +02:00
$data[$expense->category_id]['tax'] += $expense->tax;
2022-05-11 08:29:56 +02:00
}
2022-05-13 05:07:42 +02:00
$this->expense_break_down = $data;
return $this;
2022-05-11 08:29:56 +02:00
}
2022-05-11 14:38:19 +02:00
private function getTax($expense)
2022-05-11 08:29:56 +02:00
{
2022-05-11 14:38:19 +02:00
$amount = $expense->amount;
//is amount tax
if ($expense->calculate_tax_by_amount) {
2022-05-12 02:57:58 +02:00
return $expense->tax_amount1 + $expense->tax_amount2 + $expense->tax_amount3;
2022-05-11 08:29:56 +02:00
}
2022-05-11 14:38:19 +02:00
if ($expense->uses_inclusive_taxes) {
2022-05-12 02:57:58 +02:00
$inclusive = 0;
$inclusive += ($amount - ($amount / (1 + ($expense->tax_rate1 / 100))));
$inclusive += ($amount - ($amount / (1 + ($expense->tax_rate2 / 100))));
$inclusive += ($amount - ($amount / (1 + ($expense->tax_rate3 / 100))));
return round($inclusive, 2);
2022-05-12 02:57:58 +02:00
}
$exclusive = 0;
$exclusive += $amount * ($expense->tax_rate1 / 100);
$exclusive += $amount * ($expense->tax_rate2 / 100);
$exclusive += $amount * ($expense->tax_rate3 / 100);
return $exclusive;
2022-05-11 08:29:56 +02:00
}
2022-05-12 02:57:58 +02:00
private function getConvertedTotal($amount, $exchange_rate = 1)
2022-05-11 08:29:56 +02:00
{
return round(($amount * $exchange_rate), 2);
2022-05-11 08:29:56 +02:00
}
private function expenseCalcWithTax()
{
2023-09-04 02:27:28 +02:00
return \DB::select('
SELECT sum(expenses.amount) as amount,
IFNULL(expenses.currency_id, :company_currency) as currency_id
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
2023-09-04 02:27:28 +02:00
', ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]);
}
private function setBillingReportType()
{
if (array_key_exists('is_income_billed', $this->payload)) {
$this->is_income_billed = boolval($this->payload['is_income_billed']);
}
if (array_key_exists('is_expense_billed', $this->payload)) {
$this->is_expense_billed = boolval($this->payload['is_expense_billed']);
}
if (array_key_exists('include_tax', $this->payload)) {
2022-05-12 02:57:58 +02:00
$this->is_tax_included = boolval($this->payload['include_tax']);
}
$this->addDateRange();
return $this;
}
private function addDateRange()
{
$date_range = 'this_year';
if (array_key_exists('date_range', $this->payload)) {
$date_range = $this->payload['date_range'];
}
try {
$custom_start_date = Carbon::parse($this->payload['start_date']);
$custom_end_date = Carbon::parse($this->payload['end_date']);
} catch (\Exception $e) {
$custom_start_date = now()->startOfYear();
$custom_end_date = now();
}
switch ($date_range) {
case 'all':
$this->start_date = now()->subYears(50);
$this->end_date = now();
2023-06-19 14:40:52 +02:00
break;
case 'last7':
$this->start_date = now()->subDays(7);
$this->end_date = now();
2023-06-19 14:40:52 +02:00
break;
case 'last30':
$this->start_date = now()->subDays(30);
$this->end_date = now();
2023-06-19 14:40:52 +02:00
break;
case 'this_month':
$this->start_date = now()->startOfMonth();
$this->end_date = now();
2023-06-19 14:40:52 +02:00
break;
case 'last_month':
$this->start_date = now()->startOfMonth()->subMonth();
$this->end_date = now()->startOfMonth()->subMonth()->endOfMonth();
2023-06-19 14:40:52 +02:00
break;
case 'this_quarter':
2024-05-09 02:15:46 +02:00
$this->start_date = (new \Carbon\Carbon('0 months'))->startOfQuarter();
$this->end_date = (new \Carbon\Carbon('0 months'))->endOfQuarter();
2023-06-19 14:40:52 +02:00
break;
case 'last_quarter':
2024-05-09 02:15:46 +02:00
$this->start_date = (new \Carbon\Carbon('-3 months'))->startOfQuarter();
$this->end_date = (new \Carbon\Carbon('-3 months'))->endOfQuarter();
2023-06-19 14:40:52 +02:00
break;
case 'this_year':
$this->start_date = now()->startOfYear();
$this->end_date = now();
2023-06-19 14:40:52 +02:00
break;
case 'custom':
$this->start_date = $custom_start_date;
$this->end_date = $custom_end_date;
2023-06-19 14:40:52 +02:00
break;
default:
$this->start_date = now()->startOfYear();
$this->end_date = now();
}
return $this;
}
}