From fd91db2f6ee9c83b4f800ecf2cd84e1939049d56 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 30 Apr 2023 08:19:29 +1000 Subject: [PATCH] updates for paymentable dates on import --- app/Import/Providers/BaseImport.php | 14 +++++++- app/Repositories/PaymentRepository.php | 2 +- app/Services/Invoice/ApplyPayment.php | 10 ------ tests/Feature/UpdatePaymentTest.php | 48 ++++++++++++++++++++++---- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/Import/Providers/BaseImport.php b/app/Import/Providers/BaseImport.php index 7271a9f3dfc2..07255a979465 100644 --- a/app/Import/Providers/BaseImport.php +++ b/app/Import/Providers/BaseImport.php @@ -19,6 +19,7 @@ use App\Models\Invoice; use League\Csv\Statement; use App\Factory\QuoteFactory; use App\Factory\ClientFactory; +use Illuminate\Support\Carbon; use App\Factory\InvoiceFactory; use App\Factory\PaymentFactory; use App\Import\ImportException; @@ -495,7 +496,8 @@ class BaseImport /* Make sure we don't apply any payments to invoices with a Zero Amount*/ if ($invoice->amount > 0) { - $payment_repository->save( + + $payment = $payment_repository->save( $payment_data, PaymentFactory::create( $this->company->id, @@ -503,6 +505,16 @@ class BaseImport $invoice->client_id ) ); + + $payment_date = Carbon::parse($payment->date); + + if(!$payment_date->isToday()) + { + + $payment->paymentables()->update(['created_at' => $payment_date]); + + } + } } } diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 305cdbd28bf3..a0227e7c40ed 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -201,7 +201,7 @@ class PaymentRepository extends BaseRepository * the company currency, we need to set a record. * @param $data * @param $payment - * @return + * @return Payment $payment */ public function processExchangeRates($data, $payment) { diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index a18746f0fdfd..dd43a7dc7288 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -104,16 +104,6 @@ class ApplyPayment extends AbstractService $this->invoice->service()->applyNumber()->workFlow()->touchPdf()->save(); - $transaction = [ - 'invoice' => $this->invoice->transaction_event(), - 'payment' => $this->payment->transaction_event(), - 'client' => $this->invoice->client->transaction_event(), - 'credit' => [], - 'metadata' => [], - ]; - - // TransactionLog::dispatch(TransactionEvent::INVOICE_PAYMENT_APPLIED, $transaction, $this->invoice->company->db); - return $this->invoice; } } diff --git a/tests/Feature/UpdatePaymentTest.php b/tests/Feature/UpdatePaymentTest.php index 911b6d68a24b..c3d169205a35 100644 --- a/tests/Feature/UpdatePaymentTest.php +++ b/tests/Feature/UpdatePaymentTest.php @@ -11,19 +11,20 @@ namespace Tests\Feature; -use App\Factory\InvoiceFactory; -use App\Helpers\Invoice\InvoiceSum; +use Carbon\Carbon; +use Tests\TestCase; use App\Models\Client; use App\Models\Invoice; use App\Models\Payment; +use Tests\MockAccountData; +use App\Factory\InvoiceFactory; use App\Utils\Traits\MakesHash; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Foundation\Testing\WithoutEvents; -use Illuminate\Routing\Middleware\ThrottleRequests; +use App\Helpers\Invoice\InvoiceSum; use Illuminate\Support\Facades\Session; use Illuminate\Validation\ValidationException; -use Tests\MockAccountData; -use Tests\TestCase; +use Illuminate\Foundation\Testing\WithoutEvents; +use Illuminate\Routing\Middleware\ThrottleRequests; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -36,6 +37,8 @@ class UpdatePaymentTest extends TestCase use MockAccountData; use WithoutEvents; + public $faker; + protected function setUp() :void { parent::setUp(); @@ -50,6 +53,37 @@ class UpdatePaymentTest extends TestCase $this->withoutMiddleware( ThrottleRequests::class ); + } + + public function testUpdatingPaymentableDates() + { + $this->invoice = $this->invoice->service()->markPaid()->save(); + + $payment = $this->invoice->payments->first(); + + $this->assertNotNull($payment); + + $payment->paymentables()->each(function ($pivot){ + + $this->assertTrue(Carbon::createFromTimestamp($pivot->created_at)->isToday()); + }); + + $payment->paymentables()->each(function ($pivot) { + + $pivot->created_at = now()->startOfDay()->subMonth(); + $pivot->save(); + + }); + + $payment->paymentables()->each(function ($pivot) { + + $this->assertTrue(Carbon::createFromTimestamp($pivot->created_at)->eq(now()->startOfDay()->subMonth())); + + }); + + + + } public function testUpdatePaymentClientPaidToDate()