This commit is contained in:
denisdulici 2018-04-17 16:40:52 +03:00
parent cfda5585a4
commit 18a3a3f6cb
19 changed files with 344 additions and 88 deletions

View File

@ -84,9 +84,7 @@ class Bills extends Controller
$payment_methods = Modules::getPaymentMethods();
$taxes = Tax::enabled()->get()->pluck('title', 'name');
return view('expenses.bills.show', compact('bill', 'accounts', 'currencies', 'account_currency_code', 'vendors', 'categories', 'payment_methods', 'taxes'));
return view('expenses.bills.show', compact('bill', 'accounts', 'currencies', 'account_currency_code', 'vendors', 'categories', 'payment_methods'));
}
/**
@ -148,6 +146,8 @@ class Bills extends Controller
$tax_total = 0;
$sub_total = 0;
$discount_total = 0;
$discount = $request['discount'];
$bill_item = [];
$bill_item['company_id'] = $request['company_id'];
@ -177,6 +177,11 @@ class Bills extends Controller
$tax_id = $item['tax_id'];
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
// Apply discount to tax
if ($discount) {
$tax = $tax - ($tax * ($discount / 100));
}
}
$bill_item['item_id'] = $item['item_id'];
@ -210,12 +215,21 @@ class Bills extends Controller
}
}
$request['amount'] += $sub_total + $tax_total;
$s_total = $sub_total;
// Apply discount to total
if ($discount) {
$s_discount = $s_total * ($discount / 100);
$discount_total += $s_discount;
$s_total = $s_total - $s_discount;
}
$request['amount'] = $s_total + $tax_total;
$bill->update($request->input());
// Add bill totals
$this->addTotals($bill, $request, $taxes, $sub_total, $tax_total);
$this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total);
// Add bill history
BillHistory::create([
@ -336,6 +350,8 @@ class Bills extends Controller
$taxes = [];
$tax_total = 0;
$sub_total = 0;
$discount_total = 0;
$discount = $request['discount'];
$bill_item = [];
$bill_item['company_id'] = $request['company_id'];
@ -363,6 +379,11 @@ class Bills extends Controller
$tax_id = $item['tax_id'];
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
// Apply discount to tax
if ($discount) {
$tax = $tax - ($tax * ($discount / 100));
}
}
$bill_item['item_id'] = $item['item_id'];
@ -392,7 +413,16 @@ class Bills extends Controller
}
}
$request['amount'] = $sub_total + $tax_total;
$s_total = $sub_total;
// Apply discount to total
if ($discount) {
$s_discount = $s_total * ($discount / 100);
$discount_total += $s_discount;
$s_total = $s_total - $s_discount;
}
$request['amount'] = $s_total + $tax_total;
$bill->update($request->input());
@ -407,7 +437,8 @@ class Bills extends Controller
BillTotal::where('bill_id', $bill->id)->delete();
// Add bill totals
$this->addTotals($bill, $request, $taxes, $sub_total, $tax_total);
$bill->totals()->delete();
$this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total);
// Fire the event to make it extendible
event(new BillUpdated($bill));
@ -478,9 +509,7 @@ class Bills extends Controller
$logo = $this->getLogo($bill);
$taxes = Tax::enabled()->get()->pluck('title', 'name');
return view($bill->template_path, compact('bill', 'logo', 'taxes'));
return view($bill->template_path, compact('bill', 'logo'));
}
/**
@ -496,9 +525,7 @@ class Bills extends Controller
$logo = $this->getLogo($bill);
$taxes = Tax::enabled()->get()->pluck('title', 'name');
$html = view($bill->template_path, compact('bill', 'logo', 'taxes'))->render();
$html = view($bill->template_path, compact('bill', 'logo'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -646,11 +673,11 @@ class Bills extends Controller
return $bill;
}
protected function addTotals($bill, $request, $taxes, $sub_total, $tax_total)
protected function addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total)
{
$sort_order = 1;
// Added bill total sub total
// Added bill sub total
BillTotal::create([
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
@ -662,7 +689,24 @@ class Bills extends Controller
$sort_order++;
// Added bill total taxes
// Added bill discount
if ($discount_total) {
BillTotal::create([
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'discount',
'name' => 'bills.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
// This is for total
$sub_total = $sub_total - $discount_total;
}
$sort_order++;
// Added bill taxes
if ($taxes) {
foreach ($taxes as $tax) {
BillTotal::create([
@ -678,7 +722,7 @@ class Bills extends Controller
}
}
// Added bill total total
// Added bill total
BillTotal::create([
'company_id' => $request['company_id'],
'bill_id' => $bill->id,

View File

@ -87,9 +87,7 @@ class Invoices extends Controller
$payment_methods = Modules::getPaymentMethods();
$taxes = Tax::enabled()->get()->pluck('title', 'name');
return view('incomes.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'taxes'));
return view('incomes.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods'));
}
/**
@ -153,6 +151,8 @@ class Invoices extends Controller
$tax_total = 0;
$sub_total = 0;
$discount_total = 0;
$discount = $request['discount'];
$invoice_item = [];
$invoice_item['company_id'] = $request['company_id'];
@ -192,6 +192,11 @@ class Invoices extends Controller
$tax_id = $item['tax_id'];
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
// Apply discount to tax
if ($discount) {
$tax = $tax - ($tax * ($discount / 100));
}
}
$invoice_item['item_id'] = $item['item_id'];
@ -225,12 +230,21 @@ class Invoices extends Controller
}
}
$request['amount'] = $sub_total + $tax_total;
$s_total = $sub_total;
// Apply discount to total
if ($discount) {
$s_discount = $s_total * ($discount / 100);
$discount_total += $s_discount;
$s_total = $s_total - $s_discount;
}
$request['amount'] = $s_total + $tax_total;
$invoice->update($request->input());
// Add invoice totals
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
$this->addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total);
// Add invoice history
InvoiceHistory::create([
@ -357,6 +371,8 @@ class Invoices extends Controller
$taxes = [];
$tax_total = 0;
$sub_total = 0;
$discount_total = 0;
$discount = $request['discount'];
$invoice_item = [];
$invoice_item['company_id'] = $request['company_id'];
@ -384,6 +400,11 @@ class Invoices extends Controller
$tax_id = $item['tax_id'];
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
// Apply discount to tax
if ($discount) {
$tax = $tax - ($tax * ($discount / 100));
}
}
$invoice_item['item_id'] = $item['item_id'];
@ -413,7 +434,16 @@ class Invoices extends Controller
}
}
$request['amount'] = $sub_total + $tax_total;
$s_total = $sub_total;
// Apply discount to total
if ($discount) {
$s_discount = $s_total * ($discount / 100);
$discount_total += $s_discount;
$s_total = $s_total - $s_discount;
}
$request['amount'] = $s_total + $tax_total;
$invoice->update($request->input());
@ -428,7 +458,8 @@ class Invoices extends Controller
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
// Add invoice totals
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
$invoice->totals()->delete();
$this->addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total);
// Fire the event to make it extendible
event(new InvoiceUpdated($invoice));
@ -513,11 +544,7 @@ class Invoices extends Controller
$logo = $this->getLogo();
$taxes = collect(Tax::enabled()->get())->each(function ($item) {
$item->title = $item->name . ' (' . $item->rate . '%)';
})->pluck('title', 'name');
$html = view($invoice->template_path, compact('invoice', 'logo', 'taxes'))->render();
$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -573,9 +600,7 @@ class Invoices extends Controller
$logo = $this->getLogo();
$taxes = Tax::enabled()->get()->pluck('title', 'name');
return view($invoice->template_path, compact('invoice', 'logo', 'taxes'));
return view($invoice->template_path, compact('invoice', 'logo'));
}
/**
@ -591,9 +616,7 @@ class Invoices extends Controller
$logo = $this->getLogo();
$taxes = Tax::enabled()->get()->pluck('title', 'name');
$html = view($invoice->template_path, compact('invoice', 'logo', 'taxes'))->render();
$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -783,11 +806,11 @@ class Invoices extends Controller
return $invoice;
}
protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total)
protected function addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total)
{
$sort_order = 1;
// Added invoice total sub total
// Added invoice sub total
InvoiceTotal::create([
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
@ -799,7 +822,24 @@ class Invoices extends Controller
$sort_order++;
// Added invoice total taxes
// Added invoice discount
if ($discount_total) {
InvoiceTotal::create([
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'discount',
'name' => 'invoices.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
// This is for total
$sub_total = $sub_total - $discount_total;
}
$sort_order++;
// Added invoice taxes
if ($taxes) {
foreach ($taxes as $tax) {
InvoiceTotal::create([
@ -815,7 +855,7 @@ class Invoices extends Controller
}
}
// Added invoice total total
// Added invoice total
InvoiceTotal::create([
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,

View File

@ -246,6 +246,7 @@ class Items extends Controller
{
$input_items = request('item');
$currency_code = request('currency_code');
$discount = request('discount');
if (empty($currency_code)) {
$currency_code = setting('general.default_currency');
@ -273,6 +274,12 @@ class Items extends Controller
}
$sub_total += $item_sub_total;
// Apply discount to tax
if ($discount) {
$item_tax_total = $item_tax_total - ($item_tax_total * ($discount / 100));
}
$tax_total += $item_tax_total;
$total = $item_sub_total + $item_tax_total;
@ -287,6 +294,11 @@ class Items extends Controller
$json->tax_total = money($tax_total, $currency_code, true)->format();
// Apply discount to total
if ($discount) {
$sub_total = $sub_total - ($sub_total * ($discount / 100));
}
$grand_total = $sub_total + $tax_total;
$json->grand_total = money($grand_total, $currency_code, true)->format();

View File

@ -15,6 +15,13 @@ class Bill extends Model
protected $table = 'bills';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['attachment', 'discount'];
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
/**
@ -155,4 +162,26 @@ class Bill extends Model
return $this->getMedia('attachment')->last();
}
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountAttribute()
{
$discount = 0;
foreach ($this->totals as $total) {
if ($total->code != 'discount') {
continue;
}
$discount = number_format((($total->amount * 100) / $this->amount), 0);
break;
}
return $discount;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
class BillTotal extends Model
@ -33,4 +34,45 @@ class BillTotal extends Model
{
$this->attributes['amount'] = (double) $value;
}
/**
* Get the formatted name.
*
* @return string
*/
public function getNameAttribute($value)
{
$name = $value;
$percent = 0;
// Discount
if ($this->code == 'discount') {
$name = trans($name);
$percent = $this->bill->discount;
}
// Tax
if ($this->code == 'tax') {
$rate = Tax::where('name', $name)->value('rate');
if (!empty($rate)) {
$percent = $rate;
}
}
if (!empty($percent)) {
$name .= ' (';
if (setting('general.percent_position', 'after') == 'after') {
$name .= $percent . '%';
} else {
$name .= '%' . $percent;
}
$name .= ')';
}
return $name;
}
}

View File

@ -21,7 +21,7 @@ class Invoice extends Model
*
* @var array
*/
protected $appends = ['attachment'];
protected $appends = ['attachment', 'discount'];
protected $dates = ['deleted_at', 'invoiced_at', 'due_at'];
@ -164,4 +164,26 @@ class Invoice extends Model
return $this->getMedia('attachment')->last();
}
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountAttribute()
{
$discount = 0;
foreach ($this->totals as $total) {
if ($total->code != 'discount') {
continue;
}
$discount = number_format((($total->amount * 100) / $this->amount), 0);
break;
}
return $discount;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
class InvoiceTotal extends Model
@ -33,4 +34,45 @@ class InvoiceTotal extends Model
{
$this->attributes['amount'] = (double) $value;
}
/**
* Get the formatted name.
*
* @return string
*/
public function getNameAttribute($value)
{
$name = $value;
$percent = 0;
// Discount
if ($this->code == 'discount') {
$name = trans($name);
$percent = $this->invoice->discount;
}
// Tax
if ($this->code == 'tax') {
$rate = Tax::where('name', $name)->value('rate');
if (!empty($rate)) {
$percent = $rate;
}
}
if (!empty($percent)) {
$name .= ' (';
if (setting('general.percent_position', 'after') == 'after') {
$name .= $percent . '%';
} else {
$name .= '%' . $percent;
}
$name .= ')';
}
return $name;
}
}

7
public/css/app.css vendored
View File

@ -562,4 +562,11 @@ span.picture, span.attachment {
.form-group.col-md-6 input.fake-input.form-control{
min-width: 150px;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
/* display: none; <- Crashes Chrome on hover */
-webkit-appearance: none;
margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

View File

@ -12,6 +12,7 @@ return [
'quantity' => 'Quantity',
'price' => 'Price',
'sub_total' => 'Subtotal',
'discount' => 'Discount',
'tax_total' => 'Tax Total',
'total' => 'Total',

View File

@ -12,6 +12,7 @@ return [
'quantity' => 'Quantity',
'price' => 'Price',
'sub_total' => 'Subtotal',
'discount' => 'Discount',
'tax_total' => 'Tax Total',
'total' => 'Total',

View File

@ -114,17 +114,10 @@
<tbody>
@foreach($bill->totals as $total)
@if ($total->code != 'total')
@if (($total->code == 'tax') && isset($taxes[$total->name]))
<tr>
<th>{{ $taxes[$total->name] }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@else
<tr>
<th>{{ trans($total->name) }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@endif
<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">

View File

@ -76,6 +76,15 @@
<td class="text-right" colspan="5"><strong>{{ trans('bills.sub_total') }}</strong></td>
<td class="text-right"><span id="sub-total">0</span></td>
</tr>
<tr>
<td class="text-right" style="vertical-align: middle;" colspan="5"><strong>{{ trans('bills.discount') }}</strong></td>
<td class="text-right">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-percent"></i></div>
{!! Form::number('discount', null, ['class' => 'form-control text-right']) !!}
</div>
</td>
</tr>
<tr>
<td class="text-right" colspan="5"><strong>{{ trans_choice('general.taxes', 1) }}</strong></td>
<td class="text-right"><span id="tax-total">0</span></td>
@ -255,7 +264,7 @@
url: '{{ url("items/items/totalItem") }}',
type: 'POST',
dataType: 'JSON',
data: $('#currency_code, #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(data) {
if (data) {

View File

@ -92,6 +92,15 @@
<td class="text-right" colspan="5"><strong>{{ trans('bills.sub_total') }}</strong></td>
<td class="text-right"><span id="sub-total">0</span></td>
</tr>
<tr>
<td class="text-right" style="vertical-align: middle;" colspan="5"><strong>{{ trans('bills.discount') }}</strong></td>
<td class="text-right">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-percent"></i></div>
{!! Form::number('discount', $bill->discount, ['class' => 'form-control text-right']) !!}
</div>
</td>
</tr>
<tr>
<td class="text-right" colspan="5"><strong>{{ trans_choice('general.taxes', 1) }}</strong></td>
<td class="text-right"><span id="tax-total">0</span></td>
@ -300,7 +309,7 @@
url: '{{ url("items/items/totalItem") }}',
type: 'POST',
dataType: 'JSON',
data: $('#currency_code, #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(data) {
if (data) {

View File

@ -119,17 +119,10 @@
<tbody>
@foreach ($bill->totals as $total)
@if ($total->code != 'total')
@if (($total->code == 'tax') && isset($taxes[$total->name]))
<tr>
<th>{{ $taxes[$total->name] }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@else
<tr>
<th>{{ trans($total->name) }}:</th>
<td class="text-right">@money($total->amount, $bill->currency_code, true)</td>
</tr>
@endif
<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">

View File

@ -76,6 +76,15 @@
<td class="text-right" colspan="5"><strong>{{ trans('invoices.sub_total') }}</strong></td>
<td class="text-right"><span id="sub-total">0</span></td>
</tr>
<tr>
<td class="text-right" style="vertical-align: middle;" colspan="5"><strong>{{ trans('invoices.discount') }}</strong></td>
<td class="text-right">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-percent"></i></div>
{!! Form::number('discount', null, ['class' => 'form-control text-right']) !!}
</div>
</td>
</tr>
<tr>
<td class="text-right" colspan="5"><strong>{{ trans_choice('general.taxes', 1) }}</strong></td>
<td class="text-right"><span id="tax-total">0</span></td>
@ -256,7 +265,7 @@
url: '{{ url("items/items/totalItem") }}',
type: 'POST',
dataType: 'JSON',
data: $('#currency_code, #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(data) {
if (data) {

View File

@ -91,6 +91,15 @@
<td class="text-right" colspan="5"><strong>{{ trans('invoices.sub_total') }}</strong></td>
<td class="text-right"><span id="sub-total">0</span></td>
</tr>
<tr>
<td class="text-right" style="vertical-align: middle;" colspan="5"><strong>{{ trans('invoices.discount') }}</strong></td>
<td class="text-right">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-percent"></i></div>
{!! Form::number('discount', $invoice->discount, ['class' => 'form-control text-right']) !!}
</div>
</td>
</tr>
<tr>
<td class="text-right" colspan="5"><strong>{{ trans_choice('general.taxes', 1) }}</strong></td>
<td class="text-right"><span id="tax-total">0</span></td>
@ -299,7 +308,7 @@
url: '{{ url("items/items/totalItem") }}',
type: 'POST',
dataType: 'JSON',
data: $('#currency_code, #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(data) {
if (data) {

View File

@ -114,17 +114,10 @@
<tbody>
@foreach ($invoice->totals as $total)
@if ($total->code != 'total')
@if (($total->code == 'tax') && isset($taxes[$total->name]))
<tr>
<th>{{ $taxes[$total->name] }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@else
<tr>
<th>{{ trans($total->name) }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@endif
<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">

View File

@ -121,17 +121,10 @@
<tbody>
@foreach ($invoice->totals as $total)
@if ($total->code != 'total')
@if (($total->code == 'tax') && isset($taxes[$total->name]))
<tr>
<th>{{ $taxes[$total->name] }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@else
<tr>
<th>{{ trans($total->name) }}:</th>
<td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
@endif
<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">

View File

@ -0,0 +1,8 @@
<div class="form-group {{ $col }} {{ isset($attributes['required']) ? 'required' : '' }} {{ $errors->has($name) ? 'has-error' : '' }}">
{!! Form::label($name, $text, ['class' => 'control-label']) !!}
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-{{ $icon }}"></i></div>
{!! Form::number($name, $value, array_merge(['class' => 'form-control', 'placeholder' => trans('general.form.enter', ['field' => $text])], $attributes)) !!}
</div>
{!! $errors->first($name, '<p class="help-block">:message</p>') !!}
</div>