faker = \Faker\Factory::create(); Model::reguard(); $this->makeTestData(); // $this->withoutExceptionHandling(); $this->withoutMiddleware( ThrottleRequests::class ); } public function testUnappliedPaymentsAreEnabled() { $settings = ClientSettings::defaults(); $settings->use_unapplied_payment = 'always'; $client = Client::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'settings' => $settings, ]); $this->assertEquals('always', $client->settings->use_unapplied_payment); $invoice = Invoice::factory()->for($client)->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'auto_bill_enabled' => true, 'client_id' => $client->id, ]); $invoice = $invoice->calc()->getInvoice(); $payment = Payment::factory()->for($client)->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'client_id' => $client->id, 'amount' => 100, 'applied' => 0, 'refunded' => 0, 'status_id' => Payment::STATUS_COMPLETED, 'is_deleted' => 0, ]); $invoice->service()->markSent()->save(); $this->assertGreaterThan(0, $invoice->balance); nlog($invoice->balance); try{ $invoice->service()->autoBill()->save(); } catch(\Exception $e){ } $invoice = $invoice->fresh(); $payment = $payment->fresh(); nlog($invoice->toArray()); nlog($payment->toArray()); $this->assertEquals($payment->applied, $invoice->paid_to_date); $this->assertGreaterThan(2, $invoice->status_id); $this->assertGreaterThan(0, $payment->applied); // $this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id); // $this->assertEquals(0, $invoice->balance); } public function testUnappliedPaymentsAreDisabled() { $settings = ClientSettings::defaults(); $settings->use_unapplied_payment = 'off'; $client = Client::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'settings' => $settings, ]); $this->assertEquals('off', $client->settings->use_unapplied_payment); $invoice = Invoice::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'client_id' => $client->id, 'auto_bill_enabled' => true, 'status_id' => 2 ]); $invoice = $invoice->calc()->getInvoice(); $invoice_balance = $invoice->balance; $payment = Payment::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'client_id' => $client->id, 'amount' => 100, 'applied' => 0, 'refunded' => 0, 'status_id' => Payment::STATUS_COMPLETED ]); $invoice->service()->markSent()->save(); $this->assertGreaterThan(0, $invoice->balance); try { $invoice->service()->autoBill()->save(); } catch(\Exception $e) { } $invoice = $invoice->fresh(); $payment = $payment->fresh(); $this->assertEquals($invoice_balance, $invoice->balance); $this->assertEquals(0, $payment->applied); $this->assertEquals(2, $invoice->status_id); $this->assertEquals(0, $invoice->paid_to_date); $this->assertEquals($invoice->amount, $invoice->balance); // $this->assertEquals($payment->applied, $invoice->paid_to_date); // $this->assertEquals(2, $invoice->status_id); } }