New form tax reports

This commit is contained in:
David Bomba 2025-08-05 16:24:27 +10:00
parent 6895a2c9a0
commit 897b996bc5
5 changed files with 34 additions and 57 deletions

View File

@ -39,7 +39,6 @@ class InvoiceTransactionEventEntry
*/ */
public function run($invoice) public function run($invoice)
{ {
$this->setPaidRatio($invoice); $this->setPaidRatio($invoice);
$this->payments = $invoice->payments->flatMap(function ($payment) { $this->payments = $invoice->payments->flatMap(function ($payment) {

View File

@ -66,7 +66,7 @@ class InvoiceTransactionEventEntryAccrual
'invoice_partial' => $invoice->partial ?? 0, 'invoice_partial' => $invoice->partial ?? 0,
'invoice_paid_to_date' => $invoice->paid_to_date ?? 0, 'invoice_paid_to_date' => $invoice->paid_to_date ?? 0,
'invoice_status' => $invoice->is_deleted ? 7 : $invoice->status_id, 'invoice_status' => $invoice->is_deleted ? 7 : $invoice->status_id,
'event_id' => TransactionEvent::INVOICE_UPDATED, 'event_id' => TransactionEvent::PAYMENT_CASH,
'timestamp' => now()->timestamp, 'timestamp' => now()->timestamp,
'metadata' => $this->getMetadata($invoice), 'metadata' => $this->getMetadata($invoice),
'period' => now()->endOfMonth()->format('Y-m-d'), 'period' => now()->endOfMonth()->format('Y-m-d'),

View File

@ -46,7 +46,7 @@ class PaymentTransactionEventEntry implements ShouldQueue
/** /**
*/ */
public function __construct(private Payment $payment, private array $invoice_ids, private string $db, private float $invoice_adjustment = 0, private int $is_deleted = false) public function __construct(private Payment $payment, private array $invoice_ids, private string $db, private float $invoice_adjustment = 0, private bool $is_deleted = false)
{} {}
public function handle() public function handle()

View File

@ -24,13 +24,16 @@ class TaxReport
private string $number_format; private string $number_format;
public function __construct(public Company $company, public TaxSummaryReport $tsr, public Builder $query) public function __construct(public Company $company, private string $start_date, private string $end_date)
{ {
} }
public function run() public function run()
{ {
$this->start_date = Carbon::parse($this->start_date);
$this->end_date = Carbon::parse($this->end_date);
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
App::forgetInstance('translator'); App::forgetInstance('translator');
App::setLocale($this->company->locale()); App::setLocale($this->company->locale());
@ -153,8 +156,8 @@ class TaxReport
private function buildData() private function buildData()
{ {
$start_date_instance = Carbon::parse($this->tsr->start_date); $start_date_instance = $this->start_date;
$end_date_instance = Carbon::parse($this->tsr->end_date); $end_date_instance = $this->end_date;
$this->data['invoices'] = []; $this->data['invoices'] = [];
$this->data['invoices'][] = $this->data['invoices'][] =
@ -203,11 +206,14 @@ class TaxReport
$invoice->load('transaction_events'); $invoice->load('transaction_events');
} }
nlog($invoice->transaction_events->toArray());
/** @var TransactionEvent $invoice_state */ /** @var TransactionEvent $invoice_state */
$invoice_state = $invoice->transaction_events->where('event_id', TransactionEvent::INVOICE_UPDATED)->sortByDesc('timestamp')->first(); $invoice_state = $invoice->transaction_events()->where('event_id', TransactionEvent::INVOICE_UPDATED)->where('period', now()->endOfMonth()->format('Y-m-d'))->orderBy('timestamp', 'desc')->first();
$adjustments = $invoice->transaction_events->whereIn('event_id',[TransactionEvent::PAYMENT_REFUNDED, TransactionEvent::PAYMENT_DELETED]); $payment_state = $invoice->transaction_events()->where('event_id', TransactionEvent::PAYMENT_CASH)->where('period', now()->endOfMonth()->format('Y-m-d'))->orderBy('timestamp', 'desc')->first();
$adjustments = $invoice->transaction_events()->whereIn('event_id',[TransactionEvent::PAYMENT_REFUNDED, TransactionEvent::PAYMENT_DELETED])->where('period', now()->endOfMonth()->format('Y-m-d'))->get();
if($invoice_state->event_id == TransactionEvent::INVOICE_UPDATED){
if($invoice_state && $invoice_state->event_id == TransactionEvent::INVOICE_UPDATED){
$this->data['accrual']['invoices'][] = [ $this->data['accrual']['invoices'][] = [
$invoice->number, $invoice->number,
$invoice->date, $invoice->date,
@ -218,15 +224,16 @@ class TaxReport
'payable', 'payable',
]; ];
} }
elseif($invoice_state->event_id == TransactionEvent::PAYMENT_CASH){
if($payment_state && $payment_state->event_id == TransactionEvent::PAYMENT_CASH){
$this->data['cash']['invoices'][] = [ $this->data['cash']['invoices'][] = [
$invoice->number, $invoice->number,
$invoice->date, $invoice->date,
$invoice->amount, $invoice->amount,
$invoice_state->invoice_paid_to_date, $payment_state->invoice_paid_to_date,
$invoice_state->metadata->tax_report->tax_summary->total_taxes, $payment_state->metadata->tax_report->tax_summary->total_taxes,
$invoice_state->metadata->tax_report->tax_summary->total_paid, $payment_state->metadata->tax_report->tax_summary->total_paid,
'payable', 'payable',
]; ];
@ -252,36 +259,7 @@ class TaxReport
return $this; return $this;
} }
// $offset = $this->company->timezone_offset();
// /** @var Invoice $invoice */
// foreach($this->query->cursor() as $invoice){
// if($invoice->transaction_events->count() == 0){
// (new InvoiceTransactionEventEntry())->run($invoice);
// }
// //get the invoice state as at the end of the current period.
// $invoice_state =$invoice->transaction_events()
// ->where('period', Carbon::parse($invoice->date)->endOfMonth()->format('Y-m-d'))
// ->where('event_id', TransactionEvent::INVOICE_UPDATED)
// ->latest()
// ->first();
// //anything period the reporting period is considered an ADJUSTMENT
// }
// Invoice::withTrashed()
// ->where('company_id', $this->company->id)
// ->whereHas('transaction_events', function ($query){
// return $query->where('period', Carbon::parse($invoice->date)->endOfMonth()->format('Y-m-d'))
// ->whereIn('event_id',[TransactionEvent::PAYMENT_REFUNDED, TransactionEvent::PAYMENT_DELETED]);
// });
// return $this;
// }
public function getXlsFile() public function getXlsFile()
{ {

View File

@ -18,6 +18,7 @@ use App\Models\Account;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Models\TransactionEvent;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use App\Factory\InvoiceItemFactory; use App\Factory\InvoiceItemFactory;
use App\Services\Report\TaxSummaryReport; use App\Services\Report\TaxSummaryReport;
@ -217,9 +218,9 @@ class TaxSummaryReportTest extends TestCase
'client_id' => $this->client->id, 'client_id' => $this->client->id,
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'amount' => 0, 'amount' => 220,
'balance' => 0, 'balance' => 0,
'status_id' => 2, 'status_id' => 1,
'total_taxes' => 1, 'total_taxes' => 1,
'date' => now()->format('Y-m-d'), 'date' => now()->format('Y-m-d'),
'terms' => 'nada', 'terms' => 'nada',
@ -235,6 +236,7 @@ class TaxSummaryReportTest extends TestCase
]); ]);
$i = $i->calc()->getInvoice(); $i = $i->calc()->getInvoice();
$i->service()->markSent()->save();
(new InvoiceTransactionEventEntry())->run($i); (new InvoiceTransactionEventEntry())->run($i);
@ -242,7 +244,7 @@ $i2 = Invoice::factory()->create([
'client_id' => $this->client->id, 'client_id' => $this->client->id,
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'amount' => 0, 'amount' => 550,
'balance' => 0, 'balance' => 0,
'status_id' => 2, 'status_id' => 2,
'total_taxes' => 1, 'total_taxes' => 1,
@ -260,23 +262,21 @@ $i2 = Invoice::factory()->create([
]); ]);
$i2 = $i2->calc()->getInvoice(); $i2 = $i2->calc()->getInvoice();
$i2->service()->markPaid(); $i2->service()->markPaid()->save();
(new InvoiceTransactionEventEntryAccrual())->run($i2, now()->subDays(30)->format('Y-m-d'), now()->addDays(30)->format('Y-m-d')); (new InvoiceTransactionEventEntryAccrual())->run($i2, now()->subDays(30)->format('Y-m-d'), now()->addDays(30)->format('Y-m-d'));
$pl = new TaxSummaryReport($this->company, $this->payload); $tr = new \App\Services\Report\XLS\TaxReport($this->company, '2025-01-01', '2025-12-31');
$query = Invoice::query()
->withTrashed()
->where('company_id', $this->company->id)
->whereIn('status_id', [2,3,4])
->where('is_deleted', 0);
$tr = new \App\Services\Report\XLS\TaxReport($this->company, $pl, $query);
$response = $tr->run()->getXlsFile(); $response = $tr->run()->getXlsFile();
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertNotNull(TransactionEvent::where('invoice_id', $i->id)->first());
$this->assertNotNull(TransactionEvent::where('invoice_id', $i2->id)->first());
nlog(TransactionEvent::where('invoice_id', $i->id)->first()->toArray());
nlog(TransactionEvent::where('invoice_id', $i2->id)->first()->toArray());
$this->account->delete(); $this->account->delete();
} }