2023-04-14 06:45:42 +02: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)
|
2023-04-14 06:45:42 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Report;
|
|
|
|
|
|
|
|
use App\Export\CSV\BaseExport;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Number;
|
|
|
|
use App\Utils\Traits\MakesDates;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
|
|
|
class TaxSummaryReport extends BaseExport
|
|
|
|
{
|
|
|
|
use MakesDates;
|
|
|
|
//Name
|
|
|
|
//Invoice count
|
|
|
|
//Amount
|
|
|
|
//Amount with Tax
|
|
|
|
|
|
|
|
public Writer $csv;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 06:45:42 +02:00
|
|
|
public string $date_key = 'created_at';
|
|
|
|
|
|
|
|
public array $report_keys = [
|
|
|
|
'tax_name',
|
|
|
|
'tax_amount',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param array $input
|
|
|
|
[
|
|
|
|
'date_range',
|
|
|
|
'start_date',
|
|
|
|
'end_date',
|
|
|
|
'clients',
|
|
|
|
'client_id',
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
public function __construct(public Company $company, public array $input)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
2023-11-12 11:26:28 +01:00
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 06:45:42 +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));
|
|
|
|
|
|
|
|
$this->csv = Writer::createFromString();
|
2024-05-01 00:32:11 +02:00
|
|
|
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 06:45:42 +02:00
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
|
|
|
|
if (count($this->input['report_keys']) == 0) {
|
|
|
|
$this->input['report_keys'] = $this->report_keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = Invoice::query()
|
2023-09-03 03:40:22 +02:00
|
|
|
->withTrashed()
|
2023-04-14 06:45:42 +02:00
|
|
|
->where('company_id', $this->company->id)
|
2024-02-21 04:01:52 +01:00
|
|
|
->whereIn('status_id', [2,3,4])
|
2023-04-14 06:45:42 +02:00
|
|
|
->where('is_deleted', 0)
|
|
|
|
->orderBy('balance', 'desc');
|
|
|
|
|
2024-06-23 23:56:13 +02:00
|
|
|
$query = $this->addDateRange($query, 'invoices');
|
2023-04-14 06:45:42 +02:00
|
|
|
|
2023-11-11 22:59:44 +01:00
|
|
|
$this->csv->insertOne([ctrans('texts.tax_summary')]);
|
|
|
|
$this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]);
|
2023-11-12 11:22:29 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($this->input['date_range'] != 'all') {
|
2023-11-12 11:22:29 +01:00
|
|
|
$this->csv->insertOne([ctrans('texts.date_range'),' ',$this->translateDate($this->start_date, $this->company->date_format(), $this->company->locale()),' - ',$this->translateDate($this->end_date, $this->company->date_format(), $this->company->locale())]);
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-11 22:59:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 06:45:42 +02:00
|
|
|
$query = $this->filterByClients($query);
|
2023-09-03 03:40:22 +02:00
|
|
|
$accrual_map = [];
|
|
|
|
$cash_map = [];
|
2023-04-14 06:45:42 +02:00
|
|
|
|
2024-10-05 06:06:43 +02:00
|
|
|
$accrual_invoice_map = [];
|
|
|
|
$cash_invoice_map = [];
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
foreach($query->cursor() as $invoice) {
|
2023-09-03 03:40:22 +02:00
|
|
|
$calc = $invoice->calc();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-03 03:40:22 +02:00
|
|
|
//Combine the line taxes with invoice taxes here to get a total tax amount
|
|
|
|
$taxes = array_merge($calc->getTaxMap()->merge($calc->getTotalTaxMap())->toArray());
|
2023-04-14 06:45:42 +02:00
|
|
|
|
2023-09-03 03:40:22 +02:00
|
|
|
//filter into two arrays for accrual + cash
|
2023-10-26 04:57:44 +02:00
|
|
|
foreach($taxes as $tax) {
|
2023-04-14 06:45:42 +02:00
|
|
|
$key = $tax['name'];
|
2024-10-05 06:06:43 +02:00
|
|
|
$tax_prorata = 0;
|
2023-04-14 06:45:42 +02:00
|
|
|
|
2023-09-03 03:40:22 +02:00
|
|
|
if(!isset($accrual_map[$key])) {
|
|
|
|
$accrual_map[$key]['tax_amount'] = 0;
|
2023-04-14 06:45:42 +02:00
|
|
|
}
|
|
|
|
|
2023-09-03 03:40:22 +02:00
|
|
|
$accrual_map[$key]['tax_amount'] += $tax['total'];
|
2024-10-05 06:06:43 +02:00
|
|
|
$accrual_invoice_map[] = [
|
|
|
|
'number' => ctrans('texts.invoice') . " " . $invoice->number,
|
|
|
|
'date' => $this->translateDate($invoice->date, $this->company->date_format(), $this->company->locale()),
|
|
|
|
'formatted' => Number::formatMoney($tax['total'], $this->company),
|
|
|
|
'tax' => Number::formatValue($tax['total'], $this->company->currency()),
|
|
|
|
];
|
2023-09-03 03:40:22 +02:00
|
|
|
|
|
|
|
//cash
|
2023-09-03 12:57:18 +02:00
|
|
|
$key = $tax['name'];
|
2023-09-03 03:40:22 +02:00
|
|
|
|
2023-09-03 12:57:18 +02:00
|
|
|
if(!isset($cash_map[$key])) {
|
|
|
|
$cash_map[$key]['tax_amount'] = 0;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(in_array($invoice->status_id, [Invoice::STATUS_PARTIAL,Invoice::STATUS_PAID])) {
|
2023-04-14 06:45:42 +02:00
|
|
|
|
2023-10-28 00:20:37 +02:00
|
|
|
try {
|
|
|
|
if($invoice->status_id == Invoice::STATUS_PAID) {
|
2024-10-05 06:06:43 +02:00
|
|
|
$tax_prorata = $tax['total'];
|
2023-10-28 00:20:37 +02:00
|
|
|
$cash_map[$key]['tax_amount'] += $tax['total'];
|
|
|
|
} else {
|
2024-10-05 06:06:43 +02:00
|
|
|
|
|
|
|
$paid_amount = $invoice->amount - $invoice->balance;
|
|
|
|
$payment_ratio = $invoice->amount > 0 ? $paid_amount / $invoice->amount : 0;
|
|
|
|
$tax_prorata = round($payment_ratio * ($tax['total'] ?? 0),2);
|
|
|
|
|
|
|
|
$cash_map[$key]['tax_amount'] += $tax_prorata;
|
2023-10-28 00:20:37 +02:00
|
|
|
}
|
2024-10-05 06:06:43 +02:00
|
|
|
|
|
|
|
$cash_invoice_map[] = [
|
|
|
|
'number' => ctrans('texts.invoice') . " " . $invoice->number,
|
|
|
|
'date' => $this->translateDate($invoice->date, $this->company->date_format(), $this->company->locale()),
|
|
|
|
'formatted' => Number::formatMoney($tax_prorata, $this->company),
|
|
|
|
'tax' => Number::formatValue($tax_prorata, $this->company->currency()),
|
|
|
|
|
|
|
|
];
|
|
|
|
|
2023-10-28 00:20:37 +02:00
|
|
|
} catch(\DivisionByZeroError $e) {
|
|
|
|
$cash_map[$key]['tax_amount'] += 0;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-09-03 03:40:22 +02:00
|
|
|
}
|
2023-04-14 06:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-03 03:40:22 +02:00
|
|
|
$this->csv->insertOne([]);
|
2023-09-03 12:57:18 +02:00
|
|
|
$this->csv->insertOne($this->buildHeader());
|
2024-10-05 06:10:07 +02:00
|
|
|
$this->csv->insertOne([ctrans('texts.cash_vs_accrual')]);
|
2023-09-03 12:57:18 +02:00
|
|
|
|
2024-10-05 06:14:52 +02:00
|
|
|
foreach($accrual_map as $key => $value) {
|
2024-10-05 06:06:43 +02:00
|
|
|
$this->csv->insertOne([$key, Number::formatMoney($value['tax_amount'], $this->company), Number::formatValue($value['tax_amount'], $this->company->currency())]);
|
2023-09-03 03:40:22 +02:00
|
|
|
}
|
|
|
|
|
2024-10-05 06:10:07 +02:00
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([ctrans('texts.cash_accounting')]);
|
2023-09-03 12:57:18 +02:00
|
|
|
$this->csv->insertOne($this->buildHeader());
|
2023-09-03 03:40:22 +02:00
|
|
|
|
2024-10-05 06:14:52 +02:00
|
|
|
foreach($cash_map as $key => $value) {
|
2024-10-05 06:06:43 +02:00
|
|
|
$this->csv->insertOne([$key, Number::formatMoney($value['tax_amount'], $this->company), Number::formatValue($value['tax_amount'], $this->company->currency())]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
2024-10-05 06:10:07 +02:00
|
|
|
$this->csv->insertOne([ctrans('texts.cash_vs_accrual'), ctrans('texts.date'), ctrans('texts.amount'), ctrans('texts.amount')]);
|
|
|
|
|
2024-10-05 06:06:43 +02:00
|
|
|
foreach($accrual_invoice_map as $map){
|
|
|
|
$this->csv->insertOne($map);
|
2023-04-14 06:45:42 +02:00
|
|
|
}
|
|
|
|
|
2024-10-05 06:06:43 +02:00
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
2024-10-05 06:10:07 +02:00
|
|
|
$this->csv->insertOne([ctrans('texts.cash_accounting'), ctrans('texts.date'), ctrans('texts.amount'), ctrans('texts.amount')]);
|
2024-10-05 06:06:43 +02:00
|
|
|
|
|
|
|
foreach($cash_invoice_map as $map){
|
|
|
|
$this->csv->insertOne($map);
|
|
|
|
}
|
2023-04-14 06:45:42 +02:00
|
|
|
|
|
|
|
return $this->csv->toString();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 06:45:42 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
|
|
|
|
public function buildHeader(): array
|
2023-04-14 06:45:42 +02:00
|
|
|
{
|
|
|
|
$header = [];
|
|
|
|
|
|
|
|
foreach ($this->input['report_keys'] as $value) {
|
|
|
|
|
|
|
|
$header[] = ctrans("texts.{$value}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|