Capture snapshot of invoice when payment is entered

This commit is contained in:
Hillel Coren 2017-01-08 23:34:44 +02:00
parent 0911b0ab7d
commit 83d2935573
5 changed files with 38 additions and 5 deletions

View File

@ -15,6 +15,7 @@ use App\Models\Client;
use App\Models\Account; use App\Models\Account;
use App\Models\Product; use App\Models\Product;
use App\Models\Expense; use App\Models\Expense;
use App\Models\Payment;
use App\Models\TaxRate; use App\Models\TaxRate;
use App\Models\InvoiceDesign; use App\Models\InvoiceDesign;
use App\Models\Activity; use App\Models\Activity;
@ -549,6 +550,8 @@ class InvoiceController extends BaseController
public function invoiceHistory(InvoiceRequest $request) public function invoiceHistory(InvoiceRequest $request)
{ {
$invoice = $request->entity(); $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->load('user', 'invoice_items', 'documents', 'expenses', 'expenses.documents', 'account.country', 'client.contacts', 'client.country');
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date); $invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
$invoice->due_date = Utils::fromSqlDate($invoice->due_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); $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) $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) ->where('invoice_id', '=', $invoice->id)
->orderBy('id', 'desc') ->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 = []; $versionsJson = [];
$versionsSelect = []; $versionsSelect = [];
$lastId = false; $lastId = false;
$selectedId = false;
foreach ($activities as $activity) { foreach ($activities as $activity) {
if ($backup = json_decode($activity->json_backup)) { if ($backup = json_decode($activity->json_backup)) {
@ -584,8 +587,11 @@ class InvoiceController extends BaseController
$versionsJson[$activity->id] = $backup; $versionsJson[$activity->id] = $backup;
$key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName(); $key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName();
$versionsSelect[$lastId ? $lastId : 0] = $key; $versionsSelect[$lastId ?: 0] = $key;
$lastId = $activity->id; $lastId = $activity->id;
if ($activity->payment_id == $paymentId && $activity->activity_type_id == ACTIVITY_TYPE_CREATE_PAYMENT) {
$selectedId = $lastId;
}
} else { } else {
Utils::logError('Failed to parse invoice backup'); Utils::logError('Failed to parse invoice backup');
} }
@ -601,6 +607,7 @@ class InvoiceController extends BaseController
'versionsSelect' => $versionsSelect, 'versionsSelect' => $versionsSelect,
'invoiceDesigns' => InvoiceDesign::getDesigns(), 'invoiceDesigns' => InvoiceDesign::getDesigns(),
'invoiceFonts' => Cache::get('fonts'), 'invoiceFonts' => Cache::get('fonts'),
'selectedId' => $selectedId,
]; ];
return View::make('invoices.history', $data); return View::make('invoices.history', $data);

View File

@ -5,6 +5,7 @@ use Session;
use Utils; use Utils;
use View; use View;
use Cache; use Cache;
use DropdownButton;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Client; use App\Models\Client;
use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\PaymentRepository;
@ -125,6 +126,10 @@ class PaymentController extends BaseController
$payment->payment_date = Utils::fromSqlDate($payment->payment_date); $payment->payment_date = Utils::fromSqlDate($payment->payment_date);
$actions = []; $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()) { if ( ! $payment->trashed()) {
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_payment')]; $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_payment')];
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_payment')]; $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_payment')];

View File

@ -2,6 +2,7 @@
use Utils; use Utils;
use Auth; use Auth;
use App\Models\Activity;
use App\Events\InvoiceWasUpdated; use App\Events\InvoiceWasUpdated;
use App\Events\InvoiceWasCreated; use App\Events\InvoiceWasCreated;
use App\Events\PaymentWasCreated; use App\Events\PaymentWasCreated;
@ -69,6 +70,13 @@ class InvoiceListener
$invoice->updateBalances($adjustment, $partial); $invoice->updateBalances($adjustment, $partial);
$invoice->updatePaidStatus(); $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();
} }
/** /**

View File

@ -340,6 +340,15 @@ class Payment extends EntityModel
return static::calcStatusLabel($this->payment_status_id, $this->payment_status->name, $amount); 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) { Payment::creating(function ($payment) {

View File

@ -49,9 +49,13 @@
@section('content') @section('content')
{!! Former::open()->addClass('form-inline')->onchange('refreshPDF()') !!} {!! Former::open()->addClass('form-inline')->onchange('refreshPDF()') !!}
{!! Former::populateField('version', $selectedId) !!}
@if (count($versionsSelect)) @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 @endif
{!! Button::primary(trans('texts.edit_' . $invoice->getEntityType()))->asLinkTo(URL::to('/' . $invoice->getEntityType() . 's/' . $invoice->public_id . '/edit'))->withAttributes(array('class' => 'pull-right')) !!} {!! Button::primary(trans('texts.edit_' . $invoice->getEntityType()))->asLinkTo(URL::to('/' . $invoice->getEntityType() . 's/' . $invoice->public_id . '/edit'))->withAttributes(array('class' => 'pull-right')) !!}