Browse Source

Add creator relation on Invoice model and nullable notes attribute

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
891ea8229c
  1. 10
      app/Entities/Invoices/Invoice.php
  2. 7
      app/Services/InvoiceDraft/InvoiceDraft.php
  3. 4
      database/factories/ModelFactory.php
  4. 8
      database/migrations/2017_10_05_162758_create_invoices_table.php
  5. 1
      resources/lang/id/invoice.php
  6. 1
      resources/views/invoices/show.blade.php
  7. 2
      tests/Feature/InvoiceEntryTest.php
  8. 10
      tests/Unit/Models/InvoiceTest.php

10
app/Entities/Invoices/Invoice.php

@ -3,11 +3,12 @@
namespace App\Entities\Invoices; namespace App\Entities\Invoices;
use App\Entities\Projects\Project; use App\Entities\Projects\Project;
use App\Entities\Users\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Invoice extends Model class Invoice extends Model
{ {
protected $guarded = ['id','created_at','updated_at'];
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $casts = ['items' => 'array']; protected $casts = ['items' => 'array'];
@ -21,13 +22,18 @@ class Invoice extends Model
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);
} }
public function creator()
{
return $this->belongsTo(User::class);
}
public function generateNewNumber() public function generateNewNumber()
{ {
$prefix = date('ym'); $prefix = date('ym');
$lastInvoice = $this->orderBy('number', 'desc')->first(); $lastInvoice = $this->orderBy('number', 'desc')->first();
if (!is_null($lastInvoice)) {
if ( ! is_null($lastInvoice)) {
$lastInvoiceNo = $lastInvoice->number; $lastInvoiceNo = $lastInvoice->number;
if (substr($lastInvoiceNo, 0, 4) == $prefix) { if (substr($lastInvoiceNo, 0, 4) == $prefix) {
return ++$lastInvoiceNo; return ++$lastInvoiceNo;

7
app/Services/InvoiceDraft/InvoiceDraft.php

@ -31,8 +31,7 @@ class InvoiceDraft
unset($this->items[$itemKey]); unset($this->items[$itemKey]);
} }
public function empty()
{
function empty() {
$this->items = []; $this->items = [];
} }
@ -48,7 +47,7 @@ class InvoiceDraft
public function updateItem($itemKey, $newItemData) public function updateItem($itemKey, $newItemData)
{ {
if (!isset($this->items[$itemKey])) {
if ( ! isset($this->items[$itemKey])) {
return; return;
} }
@ -68,7 +67,7 @@ class InvoiceDraft
$invoice->amount = $this->getTotal(); $invoice->amount = $this->getTotal();
$invoice->notes = $this->notes; $invoice->notes = $this->notes;
$invoice->status_id = 1; $invoice->status_id = 1;
$invoice->user_id = auth()->id() ?: 1;
$invoice->creator_id = auth()->id() ?: 1;
$invoice->save(); $invoice->save();

4
database/factories/ModelFactory.php

@ -103,6 +103,8 @@ $factory->define(Invoice::class, function (Faker\Generator $faker) {
'amount' => 100000, 'amount' => 100000,
'notes' => $faker->paragraph, 'notes' => $faker->paragraph,
'status_id' => 1, 'status_id' => 1,
'user_id' => 1,
'creator_id' => function () {
return factory(User::class)->create()->id;
},
]; ];
}); });

8
database/migrations/2017_10_05_162758_create_invoices_table.php

@ -1,8 +1,8 @@
<?php <?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInvoicesTable extends Migration class CreateInvoicesTable extends Migration
{ {
@ -19,9 +19,9 @@ class CreateInvoicesTable extends Migration
$table->string('number', 8)->unique(); $table->string('number', 8)->unique();
$table->text('items'); $table->text('items');
$table->unsignedInteger('amount'); $table->unsignedInteger('amount');
$table->string('notes');
$table->string('notes')->nullable();
$table->unsignedTinyInteger('status_id'); $table->unsignedTinyInteger('status_id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('creator_id');
$table->timestamps(); $table->timestamps();
}); });
} }

1
resources/lang/id/invoice.php

@ -30,6 +30,7 @@ return [
// Attributes // Attributes
'number' => 'No. Invoice', 'number' => 'No. Invoice',
'project' => 'Project', 'project' => 'Project',
'creator' => 'Dibuat Oleh',
'items' => 'Item Invoice', 'items' => 'Item Invoice',
'notes' => 'Catatan', 'notes' => 'Catatan',
'amount' => 'Tagihan', 'amount' => 'Tagihan',

1
resources/views/invoices/show.blade.php

@ -19,6 +19,7 @@
<tr><td>{{ trans('invoice.project') }}</td><td>{{ $invoice->project->name }}</td></tr> <tr><td>{{ trans('invoice.project') }}</td><td>{{ $invoice->project->name }}</td></tr>
<tr><td>{{ trans('invoice.customer') }}</td><td>{{ $invoice->project->customer->name }}</td></tr> <tr><td>{{ trans('invoice.customer') }}</td><td>{{ $invoice->project->customer->name }}</td></tr>
<tr><td>{{ trans('invoice.items_count') }}</td><td>{{ $invoice->items_count }}</td></tr> <tr><td>{{ trans('invoice.items_count') }}</td><td>{{ $invoice->items_count }}</td></tr>
<tr><td>{{ trans('invoice.creator') }}</td><td>{{ $invoice->creator->name }}</td></tr>
<tr><td>{{ trans('invoice.amount') }}</td><td class="text-right strong">{{ formatRp($invoice->amount) }}</td></tr> <tr><td>{{ trans('invoice.amount') }}</td><td class="text-right strong">{{ formatRp($invoice->amount) }}</td></tr>
</tbody> </tbody>
</table> </table>

2
tests/Feature/InvoiceEntryTest.php

@ -165,7 +165,7 @@ class InvoiceEntryTest extends TestCase
'project_id' => $project->id, 'project_id' => $project->id,
'amount' => 3000, 'amount' => 3000,
'notes' => 'Catatan', 'notes' => 'Catatan',
'user_id' => $user->id,
'creator_id' => $user->id,
'status_id' => 1, 'status_id' => 1,
]); ]);
} }

10
tests/Unit/Models/InvoiceTest.php

@ -4,6 +4,7 @@ namespace Tests\Unit\Models;
use App\Entities\Invoices\Invoice; use App\Entities\Invoices\Invoice;
use App\Entities\Projects\Project; use App\Entities\Projects\Project;
use App\Entities\Users\User;
use Tests\TestCase; use Tests\TestCase;
class InvoiceTest extends TestCase class InvoiceTest extends TestCase
@ -18,6 +19,15 @@ class InvoiceTest extends TestCase
$this->assertTrue($invoice->project instanceof Project); $this->assertTrue($invoice->project instanceof Project);
$this->assertEquals($invoice->project->id, $project->id); $this->assertEquals($invoice->project->id, $project->id);
} }
/** @test */
public function it_has_creator_relation()
{
$user = $this->adminUserSigningIn();
$invoice = factory(Invoice::class)->create(['creator_id' => $user->id]);
$this->assertTrue($invoice->creator instanceof User);
$this->assertEquals($invoice->creator->id, $user->id);
}
/** @test */ /** @test */
public function it_generates_its_own_number() public function it_generates_its_own_number()

Loading…
Cancel
Save