Skip to content

Commit

Permalink
close #252 Fixed: Invoice partial payments are unpredictable
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Mar 13, 2018
1 parent 8573232 commit 83ad45c
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 14 deletions.
11 changes: 10 additions & 1 deletion app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,22 @@ public function paymentDestroy(BillPayment $payment)
} elseif ($bill->payments()->count() > 1) {
$bill->bill_status_code = 'partial';
} else {
$bill->bill_status_code = 'draft';
$bill->bill_status_code = 'received';
}

$bill->save();

$payment->delete();

// Add invoice history
BillHistory::create([
'company_id' => $bill->company_id,
'invoice_id' => $bill->id,
'status_code' => 'delete',
'notify' => 0,
'description' => trans('general.delete') . ' ' . $payment->description,
]);

$message = trans('messages.success.deleted', ['type' => trans_choice('general.bills', 1)]);

flash($message)->success();
Expand Down
43 changes: 38 additions & 5 deletions app/Http/Controllers/Incomes/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,20 @@ public function destroy(Invoice $invoice)
*/
public function markSent(Invoice $invoice)
{
$invoice->invoice_status_code = 'sent';
$invoice->save();
if ($invoice->invoice_status_code != 'partial') {
$invoice->invoice_status_code = 'sent';

$invoice->save();
}

// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => 'sent',
'notify' => 0,
'description' => trans('invoices.mark_sent'),
]);

flash(trans('invoices.messages.marked_sent'))->success();

Expand Down Expand Up @@ -522,8 +534,20 @@ public function emailInvoice(Invoice $invoice)
unset($invoice->pdf_path);

// Mark invoice as sent
$invoice->invoice_status_code = 'sent';
$invoice->save();
if ($invoice->invoice_status_code != 'partial') {
$invoice->invoice_status_code = 'sent';

$invoice->save();
}

// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => 'sent',
'notify' => 1,
'description' => trans('invoices.send_mail'),
]);

flash(trans('invoices.messages.email_sent'))->success();

Expand Down Expand Up @@ -705,13 +729,22 @@ public function paymentDestroy(InvoicePayment $payment)
} elseif ($invoice->payments()->count() > 1) {
$invoice->invoice_status_code = 'partial';
} else {
$invoice->invoice_status_code = 'draft';
$invoice->invoice_status_code = 'sent';
}

$invoice->save();

$payment->delete();

// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => 'delete',
'notify' => 0,
'description' => trans('general.delete') . ' ' . $payment->description,
]);

$message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]);

flash($message)->success();
Expand Down
51 changes: 51 additions & 0 deletions app/Listeners/Updates/Version1115.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Listeners\Updates;

use App\Events\UpdateFinished;
use App\Models\Company\Company;
use App\Models\Income\InvoiceStatus;
use App\Models\Expense\BillStatus;
use Artisan;

class Version1115 extends Listener
{
const ALIAS = 'core';

const VERSION = '1.1.15';

/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->check($event)) {
return;
}

// Create new bill statuses
$companies = Company::all();

foreach ($companies as $company) {
$invoice = [
'company_id' => $company->id,
'name' => trans('invoices.status.delete'),
'code' => 'delete',
];

InvoiceStatus::create($invoice);

$bill = [
'company_id' => $company->id,
'name' => trans('bills.status.delete'),
'code' => 'delete',
];

BillStatus::create($bill);
}
}
}
3 changes: 3 additions & 0 deletions app/Models/Expense/BillStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function getLabelAttribute()
case 'paid':
$label = 'label-success';
break;
case 'delete':
$label = 'label-danger';
break;
case 'partial':
case 'received':
$label = 'label-warning';
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Income/InvoiceStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function getLabelAttribute()
case 'paid':
$label = 'label-success';
break;
case 'delete':
$label = 'label-danger';
break;
case 'partial':
case 'sent':
$label = 'label-warning';
Expand Down
1 change: 1 addition & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\Updates\Version112',
'App\Listeners\Updates\Version113',
'App\Listeners\Updates\Version119',
'App\Listeners\Updates\Version1115',
],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\Auth\Login',
Expand Down
5 changes: 5 additions & 0 deletions database/seeds/BillStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ private function create()
'name' => trans('bills.status.partial'),
'code' => 'partial',
],
[
'company_id' => $company_id,
'name' => trans('bills.status.delete'),
'code' => 'delete',
],
[
'company_id' => $company_id,
'name' => trans('bills.status.paid'),
Expand Down
5 changes: 5 additions & 0 deletions database/seeds/InvoiceStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ private function create()
'name' => trans('invoices.status.partial'),
'code' => 'partial',
],
[
'company_id' => $company_id,
'name' => trans('invoices.status.delete'),
'code' => 'delete',
],
[
'company_id' => $company_id,
'name' => trans('invoices.status.paid'),
Expand Down
21 changes: 17 additions & 4 deletions resources/views/expenses/bills/bill.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,23 @@
<table class="table">
<tbody>
@foreach($bill->totals as $total)
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@if ($total->code != 'total')
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@else
@if ($bill->paid)
<tr class="text-success">
<th>{{ trans('invoices.paid') }}:</th>
<td class="text-right">- @money($bill->paid, $bill->currency_code, true)</td>
</tr>
@endif
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount - $bill->paid, $bill->currency_code, true)</td>
</tr>
@endif
@endforeach
</tbody>
</table>
Expand Down
21 changes: 17 additions & 4 deletions resources/views/incomes/invoices/invoice.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,23 @@
<table class="table">
<tbody>
@foreach($invoice->totals as $total)
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@if($total->code != 'total')
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@else
@if ($invoice->paid)
<tr class="text-success">
<th>{{ trans('invoices.paid') }}:</th>
<td class="text-right">- @money($invoice->paid, $invoice->currency_code, true)</td>
</tr>
@endif
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount - $invoice->paid, $invoice->currency_code, true)</td>
</tr>
@endif
@endforeach
</tbody>
</table>
Expand Down
2 changes: 2 additions & 0 deletions resources/views/incomes/invoices/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@
<li class="divider"></li>
@endif
@permission('update-incomes-invoices')
@if($invoice->invoice_status_code != 'partial')
<li><a href="{{ url('incomes/invoices/' . $invoice->id . '/sent') }}">{{ trans('invoices.mark_sent') }}</a></li>
@endif
@endpermission
@if($invoice->customer_email)
<li><a href="{{ url('incomes/invoices/' . $invoice->id . '/email') }}">{{ trans('invoices.send_mail') }}</a></li>
Expand Down

0 comments on commit 83ad45c

Please sign in to comment.