Skip to content

Commit

Permalink
close #472 Fixed: If you create/edit invoice/bill then Validation ret…
Browse files Browse the repository at this point in the history
…urn error missing items.
  • Loading branch information
cuneytsenturk committed Sep 3, 2018
1 parent 49e9732 commit da779e8
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 72 deletions.
9 changes: 5 additions & 4 deletions app/Http/Controllers/Common/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Common\Item as Request;
use App\Http\Requests\Common\TotalItem as TRequest;
use App\Models\Common\Item;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
Expand Down Expand Up @@ -300,11 +301,11 @@ public function autocomplete()
return response()->json($items);
}

public function totalItem()
public function totalItem(TRequest $request)
{
$input_items = request('item');
$currency_code = request('currency_code');
$discount = request('discount');
$input_items = $request->input('item');
$currency_code = $request->input('currency_code');
$discount = $request->input('discount');

if (empty($currency_code)) {
$currency_code = setting('general.default_currency');
Expand Down
42 changes: 42 additions & 0 deletions app/Http/Requests/Common/TotalItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Requests\Common;

use App\Http\Requests\Request;

class TotalItem extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'item.*.quantity' => 'required',
'item.*.price' => 'required|amount',
'item.*.currency' => 'required|string|currency',
];
}

public function messages()
{
return [
'item.*.quantity.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.quantity'))]),
'item.*.price.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.price'))]),
'item.*.currency.required' => trans('validation.custom.invalid_currency'),
'item.*.currency.string' => trans('validation.custom.invalid_currency'),
];
}
}
15 changes: 15 additions & 0 deletions app/Http/Requests/Expense/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function rules()
'billed_at' => 'required|date',
'due_at' => 'required|date',
'amount' => 'required',
'item.*.name' => 'required|string',
'item.*.quantity' => 'required',
'item.*.price' => 'required|amount',
'item.*.currency' => 'required|string|currency',
'currency_code' => 'required|string|currency',
'currency_rate' => 'required',
'vendor_id' => 'required|integer',
Expand All @@ -60,4 +64,15 @@ public function withValidator($validator)
$this->request->set('due_at', $due_at);
}
}

public function messages()
{
return [
'item.*.name.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('general.name'))]),
'item.*.quantity.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('bills.quantity'))]),
'item.*.price.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('bills.price'))]),
'item.*.currency.required' => trans('validation.custom.invalid_currency'),
'item.*.currency.string' => trans('validation.custom.invalid_currency'),
];
}
}
15 changes: 15 additions & 0 deletions app/Http/Requests/Income/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function rules()
'invoiced_at' => 'required|date',
'due_at' => 'required|date',
'amount' => 'required',
'item.*.name' => 'required|string',
'item.*.quantity' => 'required',
'item.*.price' => 'required|amount',
'item.*.currency' => 'required|string|currency',
'currency_code' => 'required|string|currency',
'currency_rate' => 'required',
'customer_id' => 'required|integer',
Expand All @@ -60,4 +64,15 @@ public function withValidator($validator)
$this->request->set('due_at', $due_at);
}
}

public function messages()
{
return [
'item.*.name.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('general.name'))]),
'item.*.quantity.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.quantity'))]),
'item.*.price.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.price'))]),
'item.*.currency.required' => trans('validation.custom.invalid_currency'),
'item.*.currency.string' => trans('validation.custom.invalid_currency'),
];
}
}
36 changes: 24 additions & 12 deletions resources/views/expenses/bills/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@
</tr>
</thead>
<tbody>
<?php $item_row = 0; ?>
@include('expenses.bills.item')
<?php $item_row++; ?>
@php $item_row = 0; @endphp
@if(old('item'))
@foreach(old('item') as $old_item)
@php $item = (object) $old_item; @endphp
@include('expenses.bills.item')
@php $item_row++; @endphp
@endforeach
@else
@include('expenses.bills.item')
@endif
@php $item_row++; @endphp
@stack('add_item_td_start')
<tr id="addItem">
<td class="text-center"><button type="button" id="button-add-item" data-toggle="tooltip" title="{{ trans('general.add') }}" class="btn btn-xs btn-primary" data-original-title="{{ trans('general.add') }}"><i class="fa fa-plus"></i></button></td>
Expand Down Expand Up @@ -122,14 +130,14 @@

{{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}

{{ Form::hidden('vendor_name', '', ['id' => 'vendor_name']) }}
{{ Form::hidden('vendor_email', '', ['id' => 'vendor_email']) }}
{{ Form::hidden('vendor_tax_number', '', ['id' => 'vendor_tax_number']) }}
{{ Form::hidden('vendor_phone', '', ['id' => 'vendor_phone']) }}
{{ Form::hidden('vendor_address', '', ['id' => 'vendor_address']) }}
{{ Form::hidden('currency_rate', '', ['id' => 'currency_rate']) }}
{{ Form::hidden('bill_status_code', 'draft', ['id' => 'bill_status_code']) }}
{{ Form::hidden('amount', '0', ['id' => 'amount']) }}
{{ Form::hidden('vendor_name', old('vendor_name'), ['id' => 'vendor_name']) }}
{{ Form::hidden('vendor_email', old('vendor_email'), ['id' => 'vendor_email']) }}
{{ Form::hidden('vendor_tax_number', old('vendor_tax_number'), ['id' => 'vendor_tax_number']) }}
{{ Form::hidden('vendor_phone', old('vendor_phone'), ['id' => 'vendor_phone']) }}
{{ Form::hidden('vendor_address', old('vendor_address'), ['id' => 'vendor_address']) }}
{{ Form::hidden('currency_rate', old('currency_rate'), ['id' => 'currency_rate']) }}
{{ Form::hidden('bill_status_code', old('bill_status_code', 'draft'), ['id' => 'bill_status_code']) }}
{{ Form::hidden('amount', old('amount', '0'), ['id' => 'amount']) }}
</div>
<!-- /.box-body -->

Expand Down Expand Up @@ -162,7 +170,7 @@
$(document).on('click', '#button-add-item', function (e) {
var currency_code = $('#currency_code').val();
$.ajax({
url: '{{ url("expenses/bills/addItem") }}',
type: 'GET',
Expand Down Expand Up @@ -415,6 +423,10 @@
}
});
});
@if(old('item'))
totalItem();
@endif
});
function totalItem() {
Expand Down
46 changes: 29 additions & 17 deletions resources/views/expenses/bills/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@
</tr>
</thead>
<tbody>
<?php $item_row = 0; ?>
@foreach($bill->items as $item)
@include('expenses.bills.item')
<?php $item_row++; ?>
@endforeach
@if (empty($bill->items))
@include('expenses.bills.item')
@endif
<?php $item_row++; ?>
@php $item_row = 0; @endphp
@if(old('item'))
@foreach(old('item') as $old_item)
@php $item = (object) $old_item; @endphp
@include('expenses.bills.item')
@php $item_row++; @endphp
@endforeach
@else
@foreach($bill->items as $item)
@include('expenses.bills.item')
@php $item_row++; @endphp
@endforeach
@if (empty($bill->items))
@include('expenses.bills.item')
@endif
@endif
@php $item_row++; @endphp
@stack('add_item_td_start')
<tr id="addItem">
<td class="text-center"><button type="button" id="button-add-item" data-toggle="tooltip" title="{{ trans('general.add') }}" class="btn btn-xs btn-primary" data-original-title="{{ trans('general.add') }}"><i class="fa fa-plus"></i></button></td>
Expand Down Expand Up @@ -104,14 +112,14 @@

{{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}

{{ Form::hidden('vendor_name', null, ['id' => 'vendor_name']) }}
{{ Form::hidden('vendor_email', null, ['id' => 'vendor_email']) }}
{{ Form::hidden('vendor_tax_number', null, ['id' => 'vendor_tax_number']) }}
{{ Form::hidden('vendor_phone', null, ['id' => 'vendor_phone']) }}
{{ Form::hidden('vendor_address', null, ['id' => 'vendor_address']) }}
{{ Form::hidden('currency_rate', null, ['id' => 'currency_rate']) }}
{{ Form::hidden('bill_status_code', null, ['id' => 'bill_status_code']) }}
{{ Form::hidden('amount', null, ['id' => 'amount']) }}
{{ Form::hidden('vendor_name', old('customer_name', null), ['id' => 'vendor_name']) }}
{{ Form::hidden('vendor_email', old('vendor_email', null), ['id' => 'vendor_email']) }}
{{ Form::hidden('vendor_tax_number', old('vendor_tax_number', null), ['id' => 'vendor_tax_number']) }}
{{ Form::hidden('vendor_phone', old('vendor_phone', null), ['id' => 'vendor_phone']) }}
{{ Form::hidden('vendor_address', old('vendor_address', null), ['id' => 'vendor_address']) }}
{{ Form::hidden('currency_rate', old('currency_rate', null), ['id' => 'currency_rate']) }}
{{ Form::hidden('bill_status_code', old('bill_status_code', null), ['id' => 'bill_status_code']) }}
{{ Form::hidden('amount', old('amount', null), ['id' => 'amount']) }}
</div>
<!-- /.box-body -->

Expand Down Expand Up @@ -423,6 +431,10 @@
}
});
});
@if(old('item'))
totalItem();
@endif
});
function totalItem() {
Expand Down
16 changes: 10 additions & 6 deletions resources/views/expenses/bills/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,43 @@
</td>
@stack('actions_td_end')
@stack('name_td_start')
<td>
<td {!! $errors->has('item.' . $item_row . '.name') ? 'class="has-error"' : '' !!}">
@stack('name_input_start')
<input value="{{ empty($item) ? '' : $item->name }}" class="form-control typeahead" required="required" placeholder="{{ trans('general.form.enter', ['field' => trans_choice('invoices.item_name', 1)]) }}" name="item[{{ $item_row }}][name]" type="text" id="item-name-{{ $item_row }}" autocomplete="off">
<input value="{{ empty($item) ? '' : $item->name }}" class="form-control typeahead" required="required" placeholder="{{ trans('general.form.enter', ['field' => trans_choice('bills.item_name', 1)]) }}" name="item[{{ $item_row }}][name]" type="text" id="item-name-{{ $item_row }}" autocomplete="off">
<input value="{{ empty($item) ? '' : $item->item_id }}" name="item[{{ $item_row }}][item_id]" type="hidden" id="item-id-{{ $item_row }}">
{!! $errors->first('item.' . $item_row . '.name', '<p class="help-block">:message</p>') !!}
@stack('name_input_end')
</td>
@stack('name_td_end')
@stack('quantity_td_start')
<td>
<td {{ $errors->has('item.' . $item_row . '.quantity') ? 'class="has-error"' : '' }}">
@stack('quantity_input_start')
<input value="{{ empty($item) ? '' : $item->quantity }}" class="form-control text-center" required="required" name="item[{{ $item_row }}][quantity]" type="text" id="item-quantity-{{ $item_row }}">
{!! $errors->first('item.' . $item_row . '.quantity', '<p class="help-block">:message</p>') !!}
@stack('quantity_input_end')
</td>
@stack('quantity_td_end')
@stack('price_td_start')
<td>
<td {{ $errors->has('item.' . $item_row . 'price') ? 'class="has-error"' : '' }}">
@stack('price_input_start')
<input value="{{ empty($item) ? '' : $item->price }}" class="form-control text-right input-price" required="required" name="item[{{ $item_row }}][price]" type="text" id="item-price-{{ $item_row }}">
<input value="{{ $currency->code }}" name="item[{{ $item_row }}][currency]" type="hidden" id="item-currency-{{ $item_row }}">
{!! $errors->first('item.' . $item_row . 'price', '<p class="help-block">:message</p>') !!}
@stack('price_input_end')
</td>
@stack('price_td_end')
@stack('taxes_td_start')
<td>
<td {{ $errors->has('item.' . $item_row . '.tax_id') ? 'class="has-error"' : '' }}">
@stack('tax_id_input_start')
{!! Form::select('item[' . $item_row . '][tax_id]', $taxes, empty($item) ? setting('general.default_tax') : $item->tax_id, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control tax-select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])]) !!}
{!! $errors->first('item.' . $item_row . '.tax_id', '<p class="help-block">:message</p>') !!}
@stack('tax_id_input_end')
</td>
@stack('taxes_td_end')
@stack('total_td_start')
<td class="text-right" style="vertical-align: middle;">
@stack('total_input_start')
@if (empty($item))
@if (empty($item) || !isset($item->total))
<span id="item-total-{{ $item_row }}">0</span>
@else
<span id="item-total-{{ $item_row }}">@money($item->total, $bill->currency_code, true)</span>
Expand Down
34 changes: 23 additions & 11 deletions resources/views/incomes/invoices/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@
</tr>
</thead>
<tbody>
<?php $item_row = 0; ?>
@include('incomes.invoices.item')
<?php $item_row++; ?>
@php $item_row = 0; @endphp
@if(old('item'))
@foreach(old('item') as $old_item)
@php $item = (object) $old_item; @endphp
@include('incomes.invoices.item')
@php $item_row++; @endphp
@endforeach
@else
@include('incomes.invoices.item')
@endif
@php $item_row++; @endphp
@stack('add_item_td_start')
<tr id="addItem">
<td class="text-center"><button type="button" id="button-add-item" data-toggle="tooltip" title="{{ trans('general.add') }}" class="btn btn-xs btn-primary" data-original-title="{{ trans('general.add') }}"><i class="fa fa-plus"></i></button></td>
Expand Down Expand Up @@ -122,14 +130,14 @@

{{ Form::fileGroup('attachment', trans('general.attachment')) }}

{{ Form::hidden('customer_name', '', ['id' => 'customer_name']) }}
{{ Form::hidden('customer_email', '', ['id' => 'customer_email']) }}
{{ Form::hidden('customer_tax_number', '', ['id' => 'customer_tax_number']) }}
{{ Form::hidden('customer_phone', '', ['id' => 'customer_phone']) }}
{{ Form::hidden('customer_address', '', ['id' => 'customer_address']) }}
{{ Form::hidden('currency_rate', '', ['id' => 'currency_rate']) }}
{{ Form::hidden('invoice_status_code', 'draft', ['id' => 'invoice_status_code']) }}
{{ Form::hidden('amount', '0', ['id' => 'amount']) }}
{{ Form::hidden('customer_name', old('customer_name'), ['id' => 'customer_name']) }}
{{ Form::hidden('customer_email', old('customer_email'), ['id' => 'customer_email']) }}
{{ Form::hidden('customer_tax_number', old('customer_tax_number'), ['id' => 'customer_tax_number']) }}
{{ Form::hidden('customer_phone', old('customer_phone'), ['id' => 'customer_phone']) }}
{{ Form::hidden('customer_address', old('customer_address'), ['id' => 'customer_address']) }}
{{ Form::hidden('currency_rate', old('currency_rate'), ['id' => 'currency_rate']) }}
{{ Form::hidden('invoice_status_code', old('invoice_status_code', 'draft'), ['id' => 'invoice_status_code']) }}
{{ Form::hidden('amount', old('amount', '0'), ['id' => 'amount']) }}
</div>
<!-- /.box-body -->

Expand Down Expand Up @@ -415,6 +423,10 @@
}
});
});
@if(old('item'))
totalItem();
@endif
});
function totalItem() {
Expand Down

0 comments on commit da779e8

Please sign in to comment.