From 83d293557370cd3f71d36748fa231e354d209d64 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 8 Jan 2017 23:34:44 +0200 Subject: [PATCH] Capture snapshot of invoice when payment is entered --- app/Http/Controllers/InvoiceController.php | 15 +++++++++++---- app/Http/Controllers/PaymentController.php | 5 +++++ app/Listeners/InvoiceListener.php | 8 ++++++++ app/Models/Payment.php | 9 +++++++++ resources/views/invoices/history.blade.php | 6 +++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index f8d8ab79c6b7..37bbe7d190f3 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -15,6 +15,7 @@ use App\Models\Client; use App\Models\Account; use App\Models\Product; use App\Models\Expense; +use App\Models\Payment; use App\Models\TaxRate; use App\Models\InvoiceDesign; use App\Models\Activity; @@ -549,6 +550,8 @@ class InvoiceController extends BaseController public function invoiceHistory(InvoiceRequest $request) { $invoice = $request->entity(); + $paymentId = $request->payment_id ? Payment::getPrivateId($request->payment_id) : false; + $invoice->load('user', 'invoice_items', 'documents', 'expenses', 'expenses.documents', 'account.country', 'client.contacts', 'client.country'); $invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date); $invoice->due_date = Utils::fromSqlDate($invoice->due_date); @@ -559,16 +562,16 @@ class InvoiceController extends BaseController ]; $invoice->invoice_type_id = intval($invoice->invoice_type_id); - $activityTypeId = $invoice->isType(INVOICE_TYPE_QUOTE) ? ACTIVITY_TYPE_UPDATE_QUOTE : ACTIVITY_TYPE_UPDATE_INVOICE; $activities = Activity::scope(false, $invoice->account_id) - ->where('activity_type_id', '=', $activityTypeId) + ->whereIn('activity_type_id', [ACTIVITY_TYPE_UPDATE_INVOICE, ACTIVITY_TYPE_UPDATE_QUOTE, ACTIVITY_TYPE_CREATE_PAYMENT]) ->where('invoice_id', '=', $invoice->id) ->orderBy('id', 'desc') - ->get(['id', 'created_at', 'user_id', 'json_backup']); + ->get(['id', 'created_at', 'user_id', 'json_backup', 'activity_type_id', 'payment_id']); $versionsJson = []; $versionsSelect = []; $lastId = false; + $selectedId = false; foreach ($activities as $activity) { if ($backup = json_decode($activity->json_backup)) { @@ -584,8 +587,11 @@ class InvoiceController extends BaseController $versionsJson[$activity->id] = $backup; $key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName(); - $versionsSelect[$lastId ? $lastId : 0] = $key; + $versionsSelect[$lastId ?: 0] = $key; $lastId = $activity->id; + if ($activity->payment_id == $paymentId && $activity->activity_type_id == ACTIVITY_TYPE_CREATE_PAYMENT) { + $selectedId = $lastId; + } } else { Utils::logError('Failed to parse invoice backup'); } @@ -601,6 +607,7 @@ class InvoiceController extends BaseController 'versionsSelect' => $versionsSelect, 'invoiceDesigns' => InvoiceDesign::getDesigns(), 'invoiceFonts' => Cache::get('fonts'), + 'selectedId' => $selectedId, ]; return View::make('invoices.history', $data); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index d59604040940..a970a601b161 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -5,6 +5,7 @@ use Session; use Utils; use View; use Cache; +use DropdownButton; use App\Models\Invoice; use App\Models\Client; use App\Ninja\Repositories\PaymentRepository; @@ -125,6 +126,10 @@ class PaymentController extends BaseController $payment->payment_date = Utils::fromSqlDate($payment->payment_date); $actions = []; + if ($payment->invoiceJsonBackup()) { + $actions[] = ['url' => url("/invoices/invoice_history/{$payment->invoice->public_id}?payment_id={$payment->public_id}"), 'label' => trans('texts.view_invoice')]; + $actions[] = DropdownButton::DIVIDER; + } if ( ! $payment->trashed()) { $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_payment')]; $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_payment')]; diff --git a/app/Listeners/InvoiceListener.php b/app/Listeners/InvoiceListener.php index 566d9b4994c2..c7353c768ad5 100644 --- a/app/Listeners/InvoiceListener.php +++ b/app/Listeners/InvoiceListener.php @@ -2,6 +2,7 @@ use Utils; use Auth; +use App\Models\Activity; use App\Events\InvoiceWasUpdated; use App\Events\InvoiceWasCreated; use App\Events\PaymentWasCreated; @@ -69,6 +70,13 @@ class InvoiceListener $invoice->updateBalances($adjustment, $partial); $invoice->updatePaidStatus(); + + // store a backup of the invoice + $activity = Activity::wherePaymentId($payment->id) + ->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT) + ->first(); + $activity->json_backup = $invoice->hidePrivateFields()->toJSON(); + $activity->save(); } /** diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 3518e345632d..4f080180603a 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -340,6 +340,15 @@ class Payment extends EntityModel return static::calcStatusLabel($this->payment_status_id, $this->payment_status->name, $amount); } + public function invoiceJsonBackup() + { + $activity = Activity::wherePaymentId($this->id) + ->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT) + ->get(['json_backup']) + ->first(); + + return $activity->json_backup; + } } Payment::creating(function ($payment) { diff --git a/resources/views/invoices/history.blade.php b/resources/views/invoices/history.blade.php index f64d1b265d6d..794806f23c2b 100644 --- a/resources/views/invoices/history.blade.php +++ b/resources/views/invoices/history.blade.php @@ -49,9 +49,13 @@ @section('content') {!! Former::open()->addClass('form-inline')->onchange('refreshPDF()') !!} + {!! Former::populateField('version', $selectedId) !!} @if (count($versionsSelect)) - {!! Former::select('version')->options($versionsSelect)->label(trans('select_version'))->style('background-color: white !important') !!} + {!! Former::select('version') + ->options($versionsSelect) + ->label(trans('select_version')) + ->style('background-color: white !important') !!} @endif {!! Button::primary(trans('texts.edit_' . $invoice->getEntityType()))->asLinkTo(URL::to('/' . $invoice->getEntityType() . 's/' . $invoice->public_id . '/edit'))->withAttributes(array('class' => 'pull-right')) !!}