Fixes for payments (#3081)

* Bump client contacts in test data

* Only allow a payment to be deleted once

* Update client balance and paid to date on payment

* Clean up
This commit is contained in:
David Bomba 2019-11-20 08:06:48 +11:00 committed by GitHub
parent ff17e3eb67
commit 6d225b7fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 77 additions and 49 deletions

View File

@ -122,6 +122,20 @@ class CreateTestData extends Command
$client = ClientFactory::create($company->id, $user->id);
$client->save();
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,rand(1,50))->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id
]);
$y = $this->count * rand(1,5);
$this->info("Creating {$y} invoices");

View File

@ -625,6 +625,10 @@ class InvoiceController extends BaseController
$invoice = MarkInvoicePaid::dispatchNow($invoice);
return $this->itemResponse($invoice);
break;
case 'mark_sent':
$invoice->markSent();
return $this->itemResponse($invoice);
break;
case 'download':
# code...
break;

View File

@ -487,6 +487,7 @@ class PaymentController extends BaseController
ReverseInvoicePayment::dispatchNow($payment);
$payment->is_deleted = true;
$payment->save();
$payment->delete();
return $this->itemResponse($payment);

View File

@ -24,21 +24,8 @@ class DestroyPaymentRequest extends Request
public function authorize() : bool
{
return auth()->user()->can('edit', $this->payment);
return auth()->user()->can('edit', $this->payment) && $this->payment->is_deleted === false;
}
// public function rules()
// {
// return [
// 'deletable'
// ];
// }
// public function messages()
// {
// return [
// 'deletable' => 'Payment cannot be deleted',
// ];
// }
}

View File

@ -44,7 +44,6 @@ class UpdateClientBalance
{
$this->client->balance += $this->amount;
$this->client->paid_to_date += $this->amount;
$this->client->save();
}

View File

@ -13,6 +13,8 @@ namespace App\Jobs\Invoice;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Invoice\ApplyPaymentToInvoice;
use App\Models\Invoice;
@ -70,6 +72,8 @@ class MarkInvoicePaid implements ShouldQueue
event(new PaymentWasCreated($payment));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
UpdateClientBalance::dispatchNow($payment->client, $this->payment->amount*-1);
UpdateClientPaidToDate::dispatchNow($payment->client, $this->payment->amount);
return $this->invoice;
}

View File

@ -12,6 +12,7 @@
namespace App\Jobs\Invoice;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger;
@ -67,7 +68,7 @@ class ReverseInvoicePayment implements ShouldQueue
UpdateClientBalance::dispatchNow($client, $this->payment->amount);
UpdateClientPaidToDate::dispatchNow($client, $this->payment->amount*-1);
}
}

View File

@ -12,6 +12,7 @@
namespace App\Jobs\Invoice;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Client\UpdateClientPaidToDate;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Jobs\Util\SystemLogger;
@ -58,15 +59,14 @@ class UpdateInvoicePayment implements ShouldQueue
$invoices->each(function ($invoice){
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
$invoice->pivot->amount = $invoice->balance;
$invoice->pivot->save();
$invoice->clearPartial();
$invoice->updateBalance($invoice->balance*-1);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
});
@ -96,7 +96,8 @@ class UpdateInvoicePayment implements ShouldQueue
if($invoice->hasPartial()) {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->partial);
$invoice->pivot->amount = $invoice->partial;
$invoice->pivot->save();
@ -105,12 +106,15 @@ class UpdateInvoicePayment implements ShouldQueue
$invoice->setDueDate();
$invoice->setStatus(Invoice::STATUS_PARTIAL);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1);
}
else
{
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
$invoice->pivot->amount = $invoice->balance;
$invoice->pivot->save();
@ -118,7 +122,7 @@ class UpdateInvoicePayment implements ShouldQueue
$invoice->clearPartial();
$invoice->updateBalance($invoice->balance*-1);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
}
@ -140,7 +144,12 @@ class UpdateInvoicePayment implements ShouldQueue
$this->payment->client
);
throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total}");
throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total} reversing payment");
$this->payment->invoice()->delete();
$this->payment->is_deleted=true;
$this->payment->save();
$this->payment->delete();
}

View File

@ -14,6 +14,7 @@ namespace App\Models;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\Currency;
use App\Models\Filterable;
@ -388,4 +389,35 @@ class Invoice extends BaseModel
$this->save();
}
public function markSent()
{
/* Return immediately if status is not draft */
if($this->status_id != Invoice::STATUS_DRAFT)
return $this;
$this->status_id = Invoice::STATUS_SENT;
$this->markInvitationsSent();
UpdateClientBalance::dispatchNow($this->client, $this->balance);
$this->save();
}
/**
* Updates Invites to SENT
*
*/
private function markInvitationsSent()
{
$this->invitations->each(function($invitation) {
if(!isset($invitation->sent_date))
{
$invitation->sent_date = Carbon::now();
$invitation->save();
}
});
}
}

View File

@ -62,6 +62,7 @@ class Payment extends BaseModel
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'is_deleted' => 'bool',
];
public function client()

View File

@ -140,15 +140,7 @@ class InvoiceRepository extends BaseRepository
*/
public function markSent(Invoice $invoice) : ?Invoice
{
/* Return immediately if status is not draft */
if($invoice->status_id != Invoice::STATUS_DRAFT)
return $invoice;
$invoice->status_id = Invoice::STATUS_SENT;
$this->markInvitationsSent($invoice);
$invoice->save();
$invoice->markSent();
/*
* Why? because up until this point the invoice was a draft.
@ -164,22 +156,6 @@ class InvoiceRepository extends BaseRepository
}
/**
* Updates Invites to SENT
*
* @param \App\Models\Invoice $invoice The invoice
*/
private function markInvitationsSent(Invoice $invoice) :void
{
$invoice->invitations->each(function($invitation) {
if(!isset($invitation->sent_date))
{
$invitation->sent_date = Carbon::now();
$invitation->save();
}
});
}
}