From 897b996bc5d31ece054125fef32bd18d3229f4c6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 5 Aug 2025 16:24:27 +1000 Subject: [PATCH] New form tax reports --- .../Invoice/InvoiceTransactionEventEntry.php | 1 - .../InvoiceTransactionEventEntryAccrual.php | 2 +- .../Payment/PaymentTransactionEventEntry.php | 2 +- app/Services/Report/XLS/TaxReport.php | 56 ++++++------------- tests/Feature/Export/TaxSummaryReportTest.php | 30 +++++----- 5 files changed, 34 insertions(+), 57 deletions(-) diff --git a/app/Listeners/Invoice/InvoiceTransactionEventEntry.php b/app/Listeners/Invoice/InvoiceTransactionEventEntry.php index 6f3da81f86..39796b9d85 100644 --- a/app/Listeners/Invoice/InvoiceTransactionEventEntry.php +++ b/app/Listeners/Invoice/InvoiceTransactionEventEntry.php @@ -39,7 +39,6 @@ class InvoiceTransactionEventEntry */ public function run($invoice) { - $this->setPaidRatio($invoice); $this->payments = $invoice->payments->flatMap(function ($payment) { diff --git a/app/Listeners/Invoice/InvoiceTransactionEventEntryAccrual.php b/app/Listeners/Invoice/InvoiceTransactionEventEntryAccrual.php index ffa3ec1679..2819c133f9 100644 --- a/app/Listeners/Invoice/InvoiceTransactionEventEntryAccrual.php +++ b/app/Listeners/Invoice/InvoiceTransactionEventEntryAccrual.php @@ -66,7 +66,7 @@ class InvoiceTransactionEventEntryAccrual 'invoice_partial' => $invoice->partial ?? 0, 'invoice_paid_to_date' => $invoice->paid_to_date ?? 0, 'invoice_status' => $invoice->is_deleted ? 7 : $invoice->status_id, - 'event_id' => TransactionEvent::INVOICE_UPDATED, + 'event_id' => TransactionEvent::PAYMENT_CASH, 'timestamp' => now()->timestamp, 'metadata' => $this->getMetadata($invoice), 'period' => now()->endOfMonth()->format('Y-m-d'), diff --git a/app/Listeners/Payment/PaymentTransactionEventEntry.php b/app/Listeners/Payment/PaymentTransactionEventEntry.php index 448dc58558..eef8272348 100644 --- a/app/Listeners/Payment/PaymentTransactionEventEntry.php +++ b/app/Listeners/Payment/PaymentTransactionEventEntry.php @@ -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() diff --git a/app/Services/Report/XLS/TaxReport.php b/app/Services/Report/XLS/TaxReport.php index 958816a22f..038071fd75 100644 --- a/app/Services/Report/XLS/TaxReport.php +++ b/app/Services/Report/XLS/TaxReport.php @@ -24,13 +24,16 @@ class TaxReport 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() { + $this->start_date = Carbon::parse($this->start_date); + $this->end_date = Carbon::parse($this->end_date); + MultiDB::setDb($this->company->db); App::forgetInstance('translator'); App::setLocale($this->company->locale()); @@ -153,8 +156,8 @@ class TaxReport private function buildData() { - $start_date_instance = Carbon::parse($this->tsr->start_date); - $end_date_instance = Carbon::parse($this->tsr->end_date); + $start_date_instance = $this->start_date; + $end_date_instance = $this->end_date; $this->data['invoices'] = []; $this->data['invoices'][] = @@ -203,11 +206,14 @@ class TaxReport $invoice->load('transaction_events'); } + nlog($invoice->transaction_events->toArray()); /** @var TransactionEvent $invoice_state */ - $invoice_state = $invoice->transaction_events->where('event_id', TransactionEvent::INVOICE_UPDATED)->sortByDesc('timestamp')->first(); - $adjustments = $invoice->transaction_events->whereIn('event_id',[TransactionEvent::PAYMENT_REFUNDED, TransactionEvent::PAYMENT_DELETED]); + $invoice_state = $invoice->transaction_events()->where('event_id', TransactionEvent::INVOICE_UPDATED)->where('period', now()->endOfMonth()->format('Y-m-d'))->orderBy('timestamp', 'desc')->first(); + $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'][] = [ $invoice->number, $invoice->date, @@ -218,15 +224,16 @@ class TaxReport 'payable', ]; } - elseif($invoice_state->event_id == TransactionEvent::PAYMENT_CASH){ + + if($payment_state && $payment_state->event_id == TransactionEvent::PAYMENT_CASH){ $this->data['cash']['invoices'][] = [ $invoice->number, $invoice->date, $invoice->amount, - $invoice_state->invoice_paid_to_date, - $invoice_state->metadata->tax_report->tax_summary->total_taxes, - $invoice_state->metadata->tax_report->tax_summary->total_paid, + $payment_state->invoice_paid_to_date, + $payment_state->metadata->tax_report->tax_summary->total_taxes, + $payment_state->metadata->tax_report->tax_summary->total_paid, 'payable', ]; @@ -252,36 +259,7 @@ class TaxReport 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() { diff --git a/tests/Feature/Export/TaxSummaryReportTest.php b/tests/Feature/Export/TaxSummaryReportTest.php index 13efe22ae6..963e8a51f0 100644 --- a/tests/Feature/Export/TaxSummaryReportTest.php +++ b/tests/Feature/Export/TaxSummaryReportTest.php @@ -18,6 +18,7 @@ use App\Models\Account; use App\Models\Company; use App\Models\Invoice; use App\Utils\Traits\MakesHash; +use App\Models\TransactionEvent; use App\DataMapper\CompanySettings; use App\Factory\InvoiceItemFactory; use App\Services\Report\TaxSummaryReport; @@ -217,9 +218,9 @@ class TaxSummaryReportTest extends TestCase 'client_id' => $this->client->id, 'user_id' => $this->user->id, 'company_id' => $this->company->id, - 'amount' => 0, + 'amount' => 220, 'balance' => 0, - 'status_id' => 2, + 'status_id' => 1, 'total_taxes' => 1, 'date' => now()->format('Y-m-d'), 'terms' => 'nada', @@ -235,14 +236,15 @@ class TaxSummaryReportTest extends TestCase ]); $i = $i->calc()->getInvoice(); - + $i->service()->markSent()->save(); + (new InvoiceTransactionEventEntry())->run($i); $i2 = Invoice::factory()->create([ 'client_id' => $this->client->id, 'user_id' => $this->user->id, 'company_id' => $this->company->id, - 'amount' => 0, + 'amount' => 550, 'balance' => 0, 'status_id' => 2, 'total_taxes' => 1, @@ -260,24 +262,22 @@ $i2 = Invoice::factory()->create([ ]); $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')); - $pl = new TaxSummaryReport($this->company, $this->payload); - - $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); + $tr = new \App\Services\Report\XLS\TaxReport($this->company, '2025-01-01', '2025-12-31'); $response = $tr->run()->getXlsFile(); $this->assertNotEmpty($response); - $this->account->delete(); + $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(); } private function buildLineItems()