mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Calculate this and last financial years
This commit is contained in:
parent
40a16226eb
commit
420fd1ef66
@ -24,8 +24,7 @@ use App\Transformers\ActivityTransformer;
|
|||||||
|
|
||||||
class ActivityExport extends BaseExport
|
class ActivityExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $entity_transformer;
|
private $entity_transformer;
|
||||||
|
|
||||||
public string $date_key = 'created_at';
|
public string $date_key = 'created_at';
|
||||||
|
@ -13,14 +13,13 @@ namespace App\Export\CSV;
|
|||||||
|
|
||||||
use App\Utils\Number;
|
use App\Utils\Number;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Models\Company;
|
||||||
use App\Models\Expense;
|
use App\Models\Expense;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\GatewayType;
|
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use League\Fractal\Manager;
|
use League\Fractal\Manager;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use App\Transformers\ClientTransformer;
|
|
||||||
use App\Transformers\PaymentTransformer;
|
use App\Transformers\PaymentTransformer;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use League\Fractal\Serializer\ArraySerializer;
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
@ -29,6 +28,8 @@ class BaseExport
|
|||||||
{
|
{
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
|
|
||||||
|
public Company $company;
|
||||||
|
|
||||||
public array $input;
|
public array $input;
|
||||||
|
|
||||||
public string $date_key = '';
|
public string $date_key = '';
|
||||||
@ -726,8 +727,15 @@ class BaseExport
|
|||||||
$this->end_date = now()->startOfDay()->format('Y-m-d');
|
$this->end_date = now()->startOfDay()->format('Y-m-d');
|
||||||
return $query->whereBetween($this->date_key, [now()->subDays(365), now()])->orderBy($this->date_key, 'ASC');
|
return $query->whereBetween($this->date_key, [now()->subDays(365), now()])->orderBy($this->date_key, 'ASC');
|
||||||
case 'this_year':
|
case 'this_year':
|
||||||
$this->start_date = now()->startOfYear()->format('Y-m-d');
|
|
||||||
$this->end_date = now()->format('Y-m-d');
|
$first_month_of_year = $this->company->getSetting('first_month_of_year') ?? 1;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
$this->start_date = $fin_year_start->format('Y-m-d');
|
||||||
|
$this->end_date = $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d');
|
||||||
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
|
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
|
||||||
case 'custom':
|
case 'custom':
|
||||||
$this->start_date = $custom_start_date->format('Y-m-d');
|
$this->start_date = $custom_start_date->format('Y-m-d');
|
||||||
|
@ -22,8 +22,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class ClientExport extends BaseExport
|
class ClientExport extends BaseExport
|
||||||
{
|
{
|
||||||
private $company;
|
|
||||||
|
|
||||||
private $client_transformer;
|
private $client_transformer;
|
||||||
|
|
||||||
private $contact_transformer;
|
private $contact_transformer;
|
||||||
|
@ -23,7 +23,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class ContactExport extends BaseExport
|
class ContactExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private ClientTransformer $client_transformer;
|
private ClientTransformer $client_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class CreditExport extends BaseExport
|
class CreditExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private CreditTransformer $credit_transformer;
|
private CreditTransformer $credit_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class DocumentExport extends BaseExport
|
class DocumentExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $entity_transformer;
|
private $entity_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class ExpenseExport extends BaseExport
|
class ExpenseExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $expense_transformer;
|
private $expense_transformer;
|
||||||
|
|
||||||
|
@ -23,8 +23,6 @@ use App\Transformers\InvoiceTransformer;
|
|||||||
|
|
||||||
class InvoiceExport extends BaseExport
|
class InvoiceExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $invoice_transformer;
|
private $invoice_transformer;
|
||||||
|
|
||||||
public string $date_key = 'date';
|
public string $date_key = 'date';
|
||||||
|
@ -22,7 +22,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class InvoiceItemExport extends BaseExport
|
class InvoiceItemExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $invoice_transformer;
|
private $invoice_transformer;
|
||||||
|
|
||||||
|
@ -21,8 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class PaymentExport extends BaseExport
|
class PaymentExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $entity_transformer;
|
private $entity_transformer;
|
||||||
|
|
||||||
public string $date_key = 'date';
|
public string $date_key = 'date';
|
||||||
|
@ -22,8 +22,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class ProductExport extends BaseExport
|
class ProductExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $entity_transformer;
|
private $entity_transformer;
|
||||||
|
|
||||||
public string $date_key = 'created_at';
|
public string $date_key = 'created_at';
|
||||||
|
@ -23,8 +23,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class ProductSalesExport extends BaseExport
|
class ProductSalesExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
public string $date_key = 'created_at';
|
public string $date_key = 'created_at';
|
||||||
|
|
||||||
protected Collection $products;
|
protected Collection $products;
|
||||||
|
@ -22,7 +22,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class PurchaseOrderExport extends BaseExport
|
class PurchaseOrderExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $purchase_order_transformer;
|
private $purchase_order_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class PurchaseOrderItemExport extends BaseExport
|
class PurchaseOrderItemExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $purchase_order_transformer;
|
private $purchase_order_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class QuoteExport extends BaseExport
|
class QuoteExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $quote_transformer;
|
private $quote_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class QuoteItemExport extends BaseExport
|
class QuoteItemExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $quote_transformer;
|
private $quote_transformer;
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class RecurringInvoiceExport extends BaseExport
|
class RecurringInvoiceExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $invoice_transformer;
|
private $invoice_transformer;
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class TaskExport extends BaseExport
|
class TaskExport extends BaseExport
|
||||||
{
|
{
|
||||||
private Company $company;
|
|
||||||
|
|
||||||
private $entity_transformer;
|
private $entity_transformer;
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ use League\Csv\Writer;
|
|||||||
|
|
||||||
class VendorExport extends BaseExport
|
class VendorExport extends BaseExport
|
||||||
{
|
{
|
||||||
private $company;
|
|
||||||
|
|
||||||
private $vendor_transformer;
|
private $vendor_transformer;
|
||||||
|
|
||||||
|
@ -121,6 +121,30 @@ trait MakesDates
|
|||||||
*/
|
*/
|
||||||
public function calculateStartAndEndDates(array $data): array
|
public function calculateStartAndEndDates(array $data): array
|
||||||
{
|
{
|
||||||
|
//override for financial years
|
||||||
|
if($data['date_range'] == 'this_year') {
|
||||||
|
$first_month_of_year = $this->company->getSetting('first_month_of_year') ?? 1;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//override for financial years
|
||||||
|
if($data['date_range'] == 'last_year') {
|
||||||
|
$first_month_of_year = $this->company->getSetting('first_month_of_year') ?? 1;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
if(now()->subYear()->lt($fin_year_start)) {
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return match ($data['date_range']) {
|
return match ($data['date_range']) {
|
||||||
EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
||||||
EmailStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
EmailStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
||||||
@ -129,7 +153,7 @@ trait MakesDates
|
|||||||
EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
|
EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
|
||||||
EmailStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')],
|
EmailStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')],
|
||||||
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')],
|
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')],
|
||||||
EmailStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')],
|
EmailStatement::THIS_YEAR => [$fin_year_start->format('Y-m-d'), $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d')],
|
||||||
EmailStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')],
|
EmailStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')],
|
||||||
EmailStatement::CUSTOM_RANGE => [$data['start_date'], $data['end_date']],
|
EmailStatement::CUSTOM_RANGE => [$data['start_date'], $data['end_date']],
|
||||||
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
|
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
|
||||||
|
@ -31,6 +31,150 @@ class DatesTest extends TestCase
|
|||||||
// $this->makeTestData();
|
// $this->makeTestData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLastFinancialYear3()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2020, 6, 30));
|
||||||
|
|
||||||
|
//override for financial years
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
if(now()->subYear()->lt($fin_year_start)) {
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals('2018-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2019-06-30', $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLastFinancialYear2()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2020, 7, 1));
|
||||||
|
|
||||||
|
//override for financial years
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
if(now()->subYear()->lt($fin_year_start)) {
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals('2019-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2020-06-30', $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLastFinancialYear()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2020, 12, 1));
|
||||||
|
|
||||||
|
//override for financial years
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
|
||||||
|
if(now()->subYear()->lt($fin_year_start)) {
|
||||||
|
$fin_year_start->subYearNoOverflow();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals('2019-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2020-06-30', $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFinancialYearDates4()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2020, 12, 1));
|
||||||
|
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYear();
|
||||||
|
|
||||||
|
$fin_year_end = $fin_year_start->copy()->addYear()->subDay();
|
||||||
|
|
||||||
|
$this->assertEquals('2020-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2021-06-30', $fin_year_end->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFinancialYearDates3()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2021, 12, 1));
|
||||||
|
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYear();
|
||||||
|
|
||||||
|
$fin_year_end = $fin_year_start->copy()->addYear()->subDay();
|
||||||
|
|
||||||
|
$this->assertEquals('2021-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2022-06-30', $fin_year_end->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFinancialYearDates2()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2021, 8, 1));
|
||||||
|
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYear();
|
||||||
|
|
||||||
|
$fin_year_end = $fin_year_start->copy()->addYear()->subDay();
|
||||||
|
|
||||||
|
$this->assertEquals('2021-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2022-06-30', $fin_year_end->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testFinancialYearDates()
|
||||||
|
{
|
||||||
|
$this->travelTo(now()->createFromDate(2021, 1, 1));
|
||||||
|
|
||||||
|
$first_month_of_year = 7;
|
||||||
|
|
||||||
|
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
|
||||||
|
|
||||||
|
if(now()->lt($fin_year_start))
|
||||||
|
$fin_year_start->subYear();
|
||||||
|
|
||||||
|
$fin_year_end = $fin_year_start->copy()->addYear()->subDay();
|
||||||
|
|
||||||
|
$this->assertEquals('2020-07-01', $fin_year_start->format('Y-m-d'));
|
||||||
|
$this->assertEquals('2021-06-30', $fin_year_end->format('Y-m-d'));
|
||||||
|
|
||||||
|
$this->travelBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function testDaysDiff()
|
public function testDaysDiff()
|
||||||
{
|
{
|
||||||
$string_date = '2021-06-01';
|
$string_date = '2021-06-01';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user