mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Merge pull request #8861 from turbo124/v5-develop
Fixes for public/private documents
This commit is contained in:
commit
f2cb0dc519
@ -112,6 +112,12 @@ class QuoteFilters extends QueryFilters
|
|||||||
->orderBy('due_date', 'DESC');
|
->orderBy('due_date', 'DESC');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(in_array('convert', $status_parameters)) {
|
||||||
|
$query->orWhere(function ($q) {
|
||||||
|
$q->whereNotNull('invoice_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this->builder;
|
return $this->builder;
|
||||||
|
@ -316,7 +316,7 @@ class Invoice extends BaseModel
|
|||||||
*/
|
*/
|
||||||
public function payments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
public function payments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
||||||
{
|
{
|
||||||
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
|
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -220,7 +220,7 @@ class Payment extends BaseModel
|
|||||||
*/
|
*/
|
||||||
public function invoices(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
public function invoices(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
||||||
{
|
{
|
||||||
return $this->morphedByMany(Invoice::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
|
return $this->morphedByMany(Invoice::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -228,7 +228,7 @@ class Payment extends BaseModel
|
|||||||
*/
|
*/
|
||||||
public function credits(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
public function credits(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
||||||
{
|
{
|
||||||
return $this->morphedByMany(Credit::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
|
return $this->morphedByMany(Credit::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,6 +96,7 @@ class EmailMailable extends Mailable
|
|||||||
|
|
||||||
$documents = Document::query()->whereIn('id', $this->email_object->documents)
|
$documents = Document::query()->whereIn('id', $this->email_object->documents)
|
||||||
->where('size', '<', $this->max_attachment_size)
|
->where('size', '<', $this->max_attachment_size)
|
||||||
|
->where('is_public',1)
|
||||||
->cursor()
|
->cursor()
|
||||||
->map(function ($document) {
|
->map(function ($document) {
|
||||||
return Attachment::fromData(fn () => $document->getFile(), $document->name);
|
return Attachment::fromData(fn () => $document->getFile(), $document->name);
|
||||||
|
@ -35,37 +35,37 @@ class ApplyPayment extends AbstractService
|
|||||||
|
|
||||||
$amount_paid = $this->payment_amount * -1;
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount) {
|
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount) {
|
||||||
//partial amount exists, but the amount is less than the partial amount
|
//partial amount exists, but the amount is less than the partial amount
|
||||||
|
|
||||||
$amount_paid = $this->payment_amount * -1;
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
$this->invoice->service()->updatePartial($amount_paid)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->updatePartial($amount_paid)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount) {
|
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount) {
|
||||||
//partial exists and the amount paid is GREATER than the partial amount
|
//partial exists and the amount paid is GREATER than the partial amount
|
||||||
|
|
||||||
$amount_paid = $this->payment_amount * -1;
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($this->payment_amount == $this->invoice->balance) {
|
if ($this->payment_amount == $this->invoice->balance) {
|
||||||
$amount_paid = $this->payment_amount * -1;
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
} elseif ($this->payment_amount < $this->invoice->balance) {
|
} elseif ($this->payment_amount < $this->invoice->balance) {
|
||||||
//partial invoice payment made
|
//partial invoice payment made
|
||||||
|
|
||||||
$amount_paid = $this->payment_amount * -1;
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
} elseif ($this->payment_amount > $this->invoice->balance) {
|
} elseif ($this->payment_amount > $this->invoice->balance) {
|
||||||
//partial invoice payment made
|
//partial invoice payment made
|
||||||
|
|
||||||
$amount_paid = $this->invoice->balance * -1;
|
$amount_paid = $this->invoice->balance * -1;
|
||||||
|
|
||||||
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->save();
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->updatePaidToDate($amount_paid*-1)->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,30 +73,16 @@ class ApplyPayment extends AbstractService
|
|||||||
->ledger()
|
->ledger()
|
||||||
->updatePaymentBalance($amount_paid);
|
->updatePaymentBalance($amount_paid);
|
||||||
|
|
||||||
// nlog("updating client balance by amount {$amount_paid}");
|
|
||||||
|
|
||||||
$this->invoice
|
$this->invoice
|
||||||
->client
|
->client
|
||||||
->service()
|
->service()
|
||||||
->updateBalance($amount_paid)
|
->updateBalance($amount_paid)
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
/* Update Pivot Record amount */
|
|
||||||
$this->payment->invoices->each(function ($inv) use ($amount_paid) {
|
|
||||||
if ($inv->id == $this->invoice->id) {
|
|
||||||
// $inv->pivot->amount = ($amount_paid * -1);
|
|
||||||
// $inv->pivot->save();
|
|
||||||
//25-06-2023
|
|
||||||
$inv->paid_to_date += floatval($amount_paid * -1);
|
|
||||||
$inv->save();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->invoice
|
$this->invoice
|
||||||
->service()
|
->service()
|
||||||
->applyNumber()
|
->applyNumber()
|
||||||
->workFlow()
|
->workFlow()
|
||||||
// ->deletePdf()
|
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
return $this->invoice;
|
return $this->invoice;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<div class="sm:flex sm:items-start sm:justify-between">
|
<div class="sm:flex sm:items-start sm:justify-between">
|
||||||
<div>
|
<div>
|
||||||
<p class="text-lg leading-6 font-medium text-gray-900">{{ ctrans('texts.attachments') }}:</p>
|
<p class="text-lg leading-6 font-medium text-gray-900">{{ ctrans('texts.attachments') }}:</p>
|
||||||
@foreach ($entity->documents as $document)
|
@foreach ($entity->documents()->where('is_public',1)->get() as $document)
|
||||||
<div class="inline-flex items-center space-x-1">
|
<div class="inline-flex items-center space-x-1">
|
||||||
@if($entity instanceof App\Models\PurchaseOrder)
|
@if($entity instanceof App\Models\PurchaseOrder)
|
||||||
<a href="{{ route('vendor.documents.download', $document->hashed_id) }}" target="_blank"
|
<a href="{{ route('vendor.documents.download', $document->hashed_id) }}" target="_blank"
|
||||||
@ -27,7 +27,7 @@
|
|||||||
</div>
|
</div>
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
||||||
@foreach ($entity->company->documents as $document)
|
@foreach ($entity->company->documents()->where('is_public',1)->get() as $document)
|
||||||
<div class="inline-flex items-center space-x-1">
|
<div class="inline-flex items-center space-x-1">
|
||||||
@if($entity instanceof App\Models\PurchaseOrder)
|
@if($entity instanceof App\Models\PurchaseOrder)
|
||||||
<a href="{{ route('vendor.documents.download', $document->hashed_id) }}" target="_blank"
|
<a href="{{ route('vendor.documents.download', $document->hashed_id) }}" target="_blank"
|
||||||
|
114
tests/Feature/Payments/DeletePaymentTest.php
Normal file
114
tests/Feature/Payments/DeletePaymentTest.php
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Feature\Payments;
|
||||||
|
|
||||||
|
use App\Factory\InvoiceFactory;
|
||||||
|
use App\Helpers\Invoice\InvoiceSum;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Credit;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Payment;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
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\MockUnitData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
class DeletePaymentTest extends TestCase
|
||||||
|
{
|
||||||
|
use MakesHash;
|
||||||
|
use DatabaseTransactions;
|
||||||
|
use MockUnitData;
|
||||||
|
|
||||||
|
protected function setUp() :void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Session::start();
|
||||||
|
|
||||||
|
$this->faker = \Faker\Factory::create();
|
||||||
|
|
||||||
|
Model::reguard();
|
||||||
|
|
||||||
|
$this->makeTestData();
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
|
||||||
|
$this->withoutMiddleware(
|
||||||
|
ThrottleRequests::class
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRegularPayment()
|
||||||
|
{
|
||||||
|
Invoice::factory()
|
||||||
|
->count(10)
|
||||||
|
->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
'amount' => 101,
|
||||||
|
'balance' => 101,
|
||||||
|
'status_id' => Invoice::STATUS_SENT,
|
||||||
|
'paid_to_date' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$i = Invoice::where('amount', 101)->where('status_id', 2)->take(1)->get();
|
||||||
|
|
||||||
|
$invoices = $i->map(function ($i){
|
||||||
|
return [
|
||||||
|
'invoice_id' => $i->hashed_id,
|
||||||
|
'amount' => $i->amount,
|
||||||
|
];
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'client_id' => $this->client->hashed_id,
|
||||||
|
'amount' => 0,
|
||||||
|
'invoices' => $invoices,
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson('/api/v1/payments/', $data);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
|
||||||
|
$arr = $response->json();
|
||||||
|
|
||||||
|
$i->fresh()->each(function ($i) {
|
||||||
|
$this->assertEquals(0, $i->balance);
|
||||||
|
$this->assertEquals(101, $i->paid_to_date);
|
||||||
|
$this->assertEquals(Invoice::STATUS_PAID, $i->status_id);
|
||||||
|
});
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson('/api/v1/payments/bulk?action=delete', ['ids' => [$arr['data']['id']]]);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
|
||||||
|
$i->fresh()->each(function ($i) {
|
||||||
|
$this->assertEquals(101, $i->balance);
|
||||||
|
$this->assertEquals(0, $i->paid_to_date);
|
||||||
|
$this->assertEquals(Invoice::STATUS_SENT, $i->status_id);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -135,6 +135,7 @@ class RefundTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals(50, $arr['data']['refunded']);
|
$this->assertEquals(50, $arr['data']['refunded']);
|
||||||
$this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']);
|
$this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -289,6 +290,12 @@ class RefundTest extends TestCase
|
|||||||
$this->assertNotNull($payment->invoices());
|
$this->assertNotNull($payment->invoices());
|
||||||
$this->assertEquals(1, $payment->invoices()->count());
|
$this->assertEquals(1, $payment->invoices()->count());
|
||||||
|
|
||||||
|
|
||||||
|
$i = $this->invoice->fresh();
|
||||||
|
|
||||||
|
$this->assertEquals(0, $i->balance);
|
||||||
|
$this->assertEquals(round($this->invoice->amount,2), round($i->paid_to_date,2));
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'id' => $this->encodePrimaryKey($payment->id),
|
'id' => $this->encodePrimaryKey($payment->id),
|
||||||
'amount' => 50,
|
'amount' => 50,
|
||||||
@ -309,6 +316,12 @@ class RefundTest extends TestCase
|
|||||||
])->post('/api/v1/payments/refund', $data);
|
])->post('/api/v1/payments/refund', $data);
|
||||||
|
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
|
|
||||||
|
$i = $this->invoice->fresh();
|
||||||
|
|
||||||
|
$this->assertEquals($i->amount, $i->balance);
|
||||||
|
$this->assertEquals(0, round($i->paid_to_date, 2));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user