Browse Source

Add validation on invoice item entry and update

Composer update laravel v5.5.22 and luthfi/formfield 0.2.3
pull/3/head
Nafies Luthfi 8 years ago
parent
commit
32c3847603
  1. 21
      app/Http/Controllers/InvoiceDraftsController.php
  2. 12
      app/Services/InvoiceDraft/InvoiceDraft.php
  3. 2
      composer.json
  4. 443
      composer.lock
  5. 2
      resources/lang/id/invoice.php
  6. 2
      resources/views/invoice-drafts/index.blade.php
  7. 21
      resources/views/invoice-drafts/partials/draft-item-list.blade.php
  8. 29
      tests/Feature/InvoiceEntryTest.php

21
app/Http/Controllers/InvoiceDraftsController.php

@ -52,7 +52,12 @@ class InvoiceDraftsController extends Controller
public function addDraftItem(Request $request, $draftKey)
{
$item = new Item(['description' => $request->description, 'amount' => $request->amount]);
$itemData = $request->validate([
'new_item_description' => 'required|string|max:255',
'new_item_amount' => 'required|numeric',
]);
$item = new Item(['description' => $itemData['new_item_description'], 'amount' => $itemData['new_item_amount']]);
$this->draftCollection->addItemToDraft($draftKey, $item);
flash(trans('invoice.item_added'));
@ -62,7 +67,19 @@ class InvoiceDraftsController extends Controller
public function updateDraftItem(Request $request, $draftKey)
{
$this->draftCollection->updateDraftItem($draftKey, $request->item_key, $request->only('description', 'amount'));
$itemData = $request->validate([
'item_key.*' => 'required|numeric',
'description.*' => 'required|string|max:255',
'amount.*' => 'required|numeric',
]);
$itemData = [
'item_key' => array_shift($itemData['item_key']),
'description' => array_shift($itemData['description']),
'amount' => array_shift($itemData['amount']),
];
$this->draftCollection->updateDraftItem($draftKey, $itemData['item_key'], $itemData);
return back();
}

12
app/Services/InvoiceDraft/InvoiceDraft.php

@ -62,13 +62,13 @@ class InvoiceDraft
public function store()
{
$invoice = new Invoice();
$invoice->number = $invoice->generateNewNumber();
$invoice->items = $this->getItemsArray();
$invoice = new Invoice();
$invoice->number = $invoice->generateNewNumber();
$invoice->items = $this->getItemsArray();
$invoice->project_id = $this->projectId;
$invoice->amount = $this->getTotal();
$invoice->notes = $this->notes;
$invoice->status_id = 1;
$invoice->amount = $this->getTotal();
$invoice->notes = $this->notes;
$invoice->status_id = 1;
$invoice->creator_id = auth()->id() ?: 1;
$invoice->save();

2
composer.json

@ -11,7 +11,7 @@
"laracasts/flash": "~2",
"laracasts/presenter": "^0.2.1",
"laravel/framework": "5.5.*",
"luthfi/formfield": "^0.2.0",
"luthfi/formfield": "^0.2.3",
"riskihajar/terbilang": "^1.2",
"spatie/laravel-fractal": "^5.0"
},

443
composer.lock
File diff suppressed because it is too large
View File

2
resources/lang/id/invoice.php

@ -29,7 +29,7 @@ return [
'undeleted' => 'Data Invoice gagal dihapus.',
'undeleteable' => 'Data Invoice tidak dapat dihapus.',
'print' => 'Cetak Invoice',
'add-item' => 'Tambah Item',
'add_item' => 'Tambah Item',
'item_added' => 'Item berhasil ditambahkan.',
'confirm_instruction' => 'Silakan periksa rincian di bawah ini, jika belum sesuai, silakan klik kembali.',

2
resources/views/invoice-drafts/index.blade.php

@ -1,6 +1,6 @@
@extends('layouts.app')
@section('title', 'Entry Invoice')
@section('title', trans('invoice.create'))
@section('content')
<h1 class="page-header">

21
resources/views/invoice-drafts/partials/draft-item-list.blade.php

@ -21,16 +21,16 @@
<td>{{ $no }}</td>
<?php $no++;?>
{{ Form::open(['route' => ['invoice-drafts.update-draft-item', $draft->draftKey], 'method' => 'patch']) }}
{{ Form::hidden('item_key', $key) }}
{{ Form::hidden('item_key['.$key.']', $key) }}
<td class="col-md-8">
{!! FormField::textarea(
'description',
'description['.$key.']',
['id' => 'description-'.$key, 'value' => $item->description, 'label' => false]
) !!}
</td>
<td class="col-md-3">
{!! FormField::price(
'amount',
'amount['.$key.']',
['id' => 'amount-'.$key, 'value' => $item->amount, 'label' => false]
) !!}
{{ Form::submit('Update', ['id' => 'update-item-'.$key, 'class' => 'btn btn-success btn-xs pull-right']) }}
@ -49,21 +49,20 @@
<th colspan="4">Tambah Item Invoice</th>
</tr>
<tr>
<td>&nbsp;</td>
{{ Form::open(['route' => ['invoice-drafts.add-draft-item', $draft->draftKey]]) }}
<td>
<td colspan="2">
{!! FormField::textarea(
'description',
['id' => 'description', 'label' => false, 'placeholder' => 'Deskripsi Item']
'new_item_description',
['id' => 'new_item_description', 'label' => false, 'placeholder' => trans('invoice.item_description')]
) !!}
</td>
<td>
<td colspan="2">
{!! FormField::price(
'amount',
['id' => 'amount', 'label' => false, 'placeholder' => 'Biaya Item']
'new_item_amount',
['id' => 'new_item_amount', 'label' => false, 'placeholder' => trans('invoice.item_amount')]
) !!}
{{ Form::submit(trans('invoice.add_item'), ['class' => 'btn btn-primary btn-block']) }}
</td>
<td class="text-center">{{ Form::submit('add-item', ['class' => 'btn btn-primary']) }}</td>
{{ Form::close() }}
</tr>
</tbody>

29
tests/Feature/InvoiceEntryTest.php

@ -9,6 +9,11 @@ use App\Services\InvoiceDrafts\InvoiceDraftCollection;
use App\Services\InvoiceDrafts\Item;
use Tests\TestCase;
/**
* Invoice Entry Feature Test
*
* @author Nafies Luthfi <nafiesl@gmail.com>
*/
class InvoiceEntryTest extends TestCase
{
/** @test */
@ -48,15 +53,15 @@ class InvoiceEntryTest extends TestCase
$this->visit(route('invoice-drafts.show', $draft->draftKey));
$this->type('Testing deskripsi invoice item', 'description');
$this->type(2000, 'amount');
$this->press('add-item');
$this->type('Testing deskripsi invoice item', 'new_item_description');
$this->type(2000, 'new_item_amount');
$this->press(trans('invoice.add_item'));
$this->see(trans('invoice.item_added'));
$this->type('Testing deskripsi invoice item', 'description');
$this->type(3000, 'amount');
$this->press('add-item');
$this->type('Testing deskripsi invoice item', 'new_item_description');
$this->type(3000, 'new_item_amount');
$this->press(trans('invoice.add_item'));
$this->seePageIs(route('invoice-drafts.show', $draft->draftKey));
$this->assertEquals(5000, $draft->getTotal());
@ -97,15 +102,15 @@ class InvoiceEntryTest extends TestCase
$this->visit(route('invoice-drafts.show', $draft->draftKey));
$this->submitForm('update-item-0', [
'item_key' => 0,
'description' => 'Testing deskripsi Update',
'amount' => 100,
'item_key[0]' => 0,
'description[0]' => 'Testing deskripsi Update',
'amount[0]' => 100,
]);
$this->submitForm('update-item-1', [
'item_key' => 1,
'description' => 'Testing deskripsi Update',
'amount' => 100,
'item_key[1]' => 1,
'description[1]' => 'Testing deskripsi Update',
'amount[1]' => 100,
]);
$this->assertEquals(200, $draft->getTotal());

Loading…
Cancel
Save