From 29b2d409f6d29d62efd65b8f68cb45fa055e4e03 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 9 Aug 2017 10:57:24 +0300 Subject: [PATCH] Support bash payments --- app/Http/Controllers/PaymentController.php | 2 +- app/Http/Requests/CreatePaymentAPIRequest.php | 2 +- app/Http/Requests/CreatePaymentRequest.php | 2 +- app/Listeners/InvoiceListener.php | 4 ++-- app/Models/Invoice.php | 8 ++++---- app/Ninja/Datatables/InvoiceDatatable.php | 4 ++-- app/Ninja/Presenters/InvoicePresenter.php | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index e0e92015920e..d63361abf0ac 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -91,7 +91,7 @@ class PaymentController extends BaseController { $invoices = Invoice::scope() ->invoices() - ->where('invoices.balance', '!=', 0) + ->where('invoices.invoice_status_id', '!=', INVOICE_STATUS_PAID) ->with('client', 'invoice_status') ->orderBy('invoice_number')->get(); diff --git a/app/Http/Requests/CreatePaymentAPIRequest.php b/app/Http/Requests/CreatePaymentAPIRequest.php index c6b4657080c3..2292c5212e24 100644 --- a/app/Http/Requests/CreatePaymentAPIRequest.php +++ b/app/Http/Requests/CreatePaymentAPIRequest.php @@ -26,7 +26,7 @@ class CreatePaymentAPIRequest extends PaymentRequest if (! $this->invoice_id || ! $this->amount) { return [ 'invoice_id' => 'required|numeric|min:1', - 'amount' => 'required|numeric|not_in:0', + 'amount' => 'required|numeric', ]; } diff --git a/app/Http/Requests/CreatePaymentRequest.php b/app/Http/Requests/CreatePaymentRequest.php index f9a912b59eec..18654f6ee2de 100644 --- a/app/Http/Requests/CreatePaymentRequest.php +++ b/app/Http/Requests/CreatePaymentRequest.php @@ -37,7 +37,7 @@ class CreatePaymentRequest extends PaymentRequest $rules = [ 'client' => 'required', // TODO: change to client_id once views are updated 'invoice' => 'required', // TODO: change to invoice_id once views are updated - 'amount' => 'required|numeric|not_in:0', + 'amount' => 'required|numeric', 'payment_date' => 'required', ]; diff --git a/app/Listeners/InvoiceListener.php b/app/Listeners/InvoiceListener.php index a4da0c9b7892..073cd87e8609 100644 --- a/app/Listeners/InvoiceListener.php +++ b/app/Listeners/InvoiceListener.php @@ -48,7 +48,7 @@ class InvoiceListener public function updatedInvoice(InvoiceWasUpdated $event) { $invoice = $event->invoice; - $invoice->updatePaidStatus(false); + $invoice->updatePaidStatus(false, false); } /** @@ -71,7 +71,7 @@ class InvoiceListener $partial = max(0, $invoice->partial - $payment->amount); $invoice->updateBalances($adjustment, $partial); - $invoice->updatePaidStatus(); + $invoice->updatePaidStatus(true); // store a backup of the invoice $activity = Activity::wherePaymentId($payment->id) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index fe46b22d50a8..3e07367344a9 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -560,12 +560,12 @@ class Invoice extends EntityModel implements BalanceAffecting /** * @param bool $save */ - public function updatePaidStatus($save = true) + public function updatePaidStatus($paid = false, $save = true) { $statusId = false; - if ($this->amount != 0 && $this->balance == 0) { + if ($paid && $this->balance == 0) { $statusId = INVOICE_STATUS_PAID; - } elseif ($this->isSent() && $this->balance > 0 && $this->balance < $this->amount) { + } elseif ($paid && $this->balance > 0 && $this->balance < $this->amount) { $statusId = INVOICE_STATUS_PARTIAL; } elseif ($this->isPartial() && $this->balance > 0) { $statusId = ($this->balance == $this->amount ? INVOICE_STATUS_SENT : INVOICE_STATUS_PARTIAL); @@ -648,7 +648,7 @@ class Invoice extends EntityModel implements BalanceAffecting public function canBePaid() { - return floatval($this->balance) != 0 && ! $this->is_deleted && $this->isStandard(); + return ! $this->isPaid() && ! $this->is_deleted && $this->isStandard(); } public static function calcStatusLabel($status, $class, $entityType, $quoteInvoiceId) diff --git a/app/Ninja/Datatables/InvoiceDatatable.php b/app/Ninja/Datatables/InvoiceDatatable.php index efbd4e9905cf..bcc31f999390 100644 --- a/app/Ninja/Datatables/InvoiceDatatable.php +++ b/app/Ninja/Datatables/InvoiceDatatable.php @@ -129,7 +129,7 @@ class InvoiceDatatable extends EntityDatatable return "javascript:submitForm_{$entityType}('markPaid', {$model->public_id})"; }, function ($model) use ($entityType) { - return $entityType == ENTITY_INVOICE && $model->balance != 0 && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]); + return $entityType == ENTITY_INVOICE && $model->invoice_status_id != INVOICE_STATUS_PAID && Auth::user()->can('editByOwner', [ENTITY_INVOICE, $model->user_id]); }, ], [ @@ -138,7 +138,7 @@ class InvoiceDatatable extends EntityDatatable return URL::to("payments/create/{$model->client_public_id}/{$model->public_id}"); }, function ($model) use ($entityType) { - return $entityType == ENTITY_INVOICE && $model->balance > 0 && Auth::user()->can('create', ENTITY_PAYMENT); + return $entityType == ENTITY_INVOICE && $model->invoice_status_id != INVOICE_STATUS_PAID && Auth::user()->can('create', ENTITY_PAYMENT); }, ], [ diff --git a/app/Ninja/Presenters/InvoicePresenter.php b/app/Ninja/Presenters/InvoicePresenter.php index 2063675aca0a..f77194ef5b71 100644 --- a/app/Ninja/Presenters/InvoicePresenter.php +++ b/app/Ninja/Presenters/InvoicePresenter.php @@ -233,7 +233,7 @@ class InvoicePresenter extends EntityPresenter $actions[] = ['url' => url("quotes/{$invoice->quote_id}/edit"), 'label' => trans('texts.view_quote')]; } - if (!$invoice->deleted_at && ! $invoice->is_recurring && $invoice->balance != 0) { + if ($invoice->canBePaid()) { $actions[] = ['url' => 'javascript:submitBulkAction("markPaid")', 'label' => trans('texts.mark_paid')]; $actions[] = ['url' => 'javascript:onPaymentClick()', 'label' => trans('texts.enter_payment')]; }