diff --git a/app/Http/ValidationRules/ValidCreditsPresentRule.php b/app/Http/ValidationRules/ValidCreditsPresentRule.php index 877a0c1d3e1d..bdec15177c8f 100644 --- a/app/Http/ValidationRules/ValidCreditsPresentRule.php +++ b/app/Http/ValidationRules/ValidCreditsPresentRule.php @@ -50,7 +50,11 @@ class ValidCreditsPresentRule implements Rule private function validCreditsPresent(): bool { if (array_key_exists('credits', $this->input) && is_array($this->input['credits']) && count($this->input['credits']) > 0) { - $credit_collection = Credit::query()->where('client_id', request()->input('client_id'))->whereIn('id', array_column($this->input['credits'], 'credit_id'))->count(); + $client_id = is_numeric(request()->input('client_id')) ?: $this->decodePrimaryKey(request()->input('client_id')); + // $credit_collection = Credit::query()->where('client_id', $client_id)->whereIn('id', array_column($this->input['credits'], 'credit_id'))->count(); + + $credit_collection = Credit::query()->whereIn('id', array_column($this->input['credits'], 'credit_id'))->count(); + return $credit_collection == count($this->input['credits']); } diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index e27c89c80dfb..690bbc68cbdf 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -11,23 +11,24 @@ namespace Tests\Feature; -use App\DataMapper\InvoiceItem; -use App\Factory\ClientFactory; -use App\Factory\CreditFactory; -use App\Factory\InvoiceFactory; -use App\Helpers\Invoice\InvoiceSum; -use App\Models\ClientContact; +use Tests\TestCase; +use App\Models\Client; use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; +use Tests\MockAccountData; +use App\Models\ClientContact; +use App\Factory\ClientFactory; +use App\Factory\CreditFactory; +use App\DataMapper\InvoiceItem; +use App\Factory\InvoiceFactory; use App\Utils\Traits\MakesHash; +use App\Helpers\Invoice\InvoiceSum; use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Session; use Illuminate\Validation\ValidationException; -use Tests\MockAccountData; -use Tests\TestCase; +use Illuminate\Routing\Middleware\ThrottleRequests; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -679,29 +680,22 @@ class RefundTest extends TestCase $this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id $this->invoice->client_id = $client->id; - $this->invoice->status_id = Invoice::STATUS_SENT; - $this->invoice->line_items = $this->buildLineItems(); $this->invoice->uses_inclusive_taxes = false; $this->invoice->client_id = $client->id; - $this->invoice->save(); - $invoice_calc = new InvoiceSum($this->invoice); - $invoice_calc->build(); - - $this->invoice = $invoice_calc->getInvoice(); - $this->invoice->save(); + $this->invoice->calc()->getInvoice()->service()->markSent()->save(); $this->credit = CreditFactory::create($this->company->id, $this->user->id); $this->credit->client_id = $client->id; - $this->credit->status_id = 2; - $this->credit->line_items = $this->buildLineItems(); - $this->credit->amount = 10; - $this->credit->balance = 10; - $this->credit->uses_inclusive_taxes = false; - $this->credit->save(); + $this->credit->date = now()->format('Y-m-d'); + $this->credit->due_date = now()->addMonth()->format('Y-m-d'); + $this->credit->calc()->getCredit()->service()->markSent()->save(); + + $this->assertEquals(10, $this->credit->amount); + $this->assertEquals(10, $this->credit->balance); $data = [ 'amount' => 50, @@ -783,26 +777,62 @@ class RefundTest extends TestCase public function testRefundsWhenCreditsArePresent() { + $cl = Client::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + ]); + + nlog($cl->id); + $i = Invoice::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, - 'client_id' => $this->client->id, + 'client_id' => $cl->id, 'status_id' => Invoice::STATUS_SENT, 'amount' => 1000, 'balance' => 1000, ]); + $item = new InvoiceItem; + $item->cost = 1000; + $item->quantity = 1; + + $i->line_items = [$item]; + + $i->service()->markSent()->save(); + + $this->assertEquals(1000, $i->balance); + $c = Credit::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, - 'client_id' => $this->client->id, + 'client_id' => $cl->id, 'status_id' => Invoice::STATUS_SENT, 'amount' => 100, 'balance' => 100, + 'date' => now()->format('Y-m-d'), + 'due_date' => now()->addMonth()->format('Y-m-d'), ]); + $item = new InvoiceItem(); + $item->cost = 100; + $item->quantity = 1; + + $c->line_items = [$item]; + + $c->service()->markSent()->save(); + + $this->assertEquals(100, $c->balance); + $this->assertNotNull($c); + $this->assertEquals(2, $c->status_id); + +$this->assertEquals($cl->id, $c->client_id); + +$this->assertEquals($cl->id, $i->client_id); + $data = [ - 'client_id' => $this->client->hashed_id, + 'amount' => 900, + 'client_id' => $cl->hashed_id, 'invoices' => [ [ 'invoice_id' => $i->hashed_id, @@ -833,7 +863,7 @@ class RefundTest extends TestCase $refund = [ 'id' => $payment_id, - 'client_id' => $this->client->hashed_id, + 'client_id' => $cl->hashed_id, 'amount' => 10, 'date' => now()->format('Y-m-d'), 'invoices' => [