From bb0cc221dd381e2179434dd9192029085b62768f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 20 Apr 2017 13:32:23 +0800 Subject: [PATCH] Added user can update item qty and item_discount on transaction draft view Added transaction draft metods --- app/Cart/Item.php | 11 ++- app/Cart/TransactionDraft.php | 5 ++ resources/views/cart/index.blade.php | 40 +++++++--- tests/Feature/TransactionEntryTest.php | 83 ++++++++++++++++++-- tests/Unit/Integration/TransactionDraftTest.php | 100 ++++++++++++++++++++++++ 5 files changed, 219 insertions(+), 20 deletions(-) diff --git a/app/Cart/Item.php b/app/Cart/Item.php index fdfb5d7..7dcc5ba 100644 --- a/app/Cart/Item.php +++ b/app/Cart/Item.php @@ -36,11 +36,16 @@ class Item } if (isset($newItemData['item_discount'])) { - $this->item_discount = $newItemData['item_discount']; - $this->item_discount_subtotal = $this->item_discount * $this->qty; - $this->subtotal = $this->subtotal - $this->item_discount_subtotal; + $this->setItemDiscount($newItemData['item_discount']); } return $this; } + + public function setItemDiscount(int $discount) + { + $this->item_discount = $discount; + $this->item_discount_subtotal = $discount * $this->qty; + $this->subtotal = $this->subtotal - $this->item_discount_subtotal; + } } diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index 0f8d055..faf0318 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -48,6 +48,11 @@ abstract class TransactionDraft $this->items = []; } + public function getSubtotal() + { + return $this->items()->sum('subtotal') + $this->getDiscountTotal(); + } + public function getTotal() { return $this->items()->sum('subtotal'); diff --git a/resources/views/cart/index.blade.php b/resources/views/cart/index.blade.php index 6c13867..fe23d6e 100644 --- a/resources/views/cart/index.blade.php +++ b/resources/views/cart/index.blade.php @@ -45,7 +45,7 @@ @forelse($queriedProducts as $product) {{ $product->name }} - {{ $draft->type == 'cash' ? $product->cash_price : $product->credit_price }} + {{ formatRp($draft->type == 'cash' ? $product->cash_price : $product->credit_price) }}
{{ csrf_field() }} @@ -73,10 +73,10 @@ # Nama Item Harga Satuan - Qty Diskon per Item - Subtotal - Action + Qty + Subtotal + Action @@ -88,28 +88,48 @@ {{ Form::open(['route' => ['cart.update-draft-item', $draft->draftKey], 'method' => 'patch']) }} {{ Form::hidden('item_key', $key) }} - {{ Form::hidden('item_discount', $item->item_discount) }} - {{ Form::number('qty', $item->qty, ['id' => 'qty-' . $key, 'style' => 'width:50px;text-align:center']) }} + {{ Form::hidden('qty', $item->qty) }} + {{ Form::text('item_discount', $item->item_discount, ['id' => 'item_discount-' . $key, 'style' => 'width:100px;text-align:right']) }} + {{ Form::submit('update-item-' . $key, ['style'=>'display:none']) }} {{ Form::close() }} {{ Form::open(['route' => ['cart.update-draft-item', $draft->draftKey], 'method' => 'patch']) }} {{ Form::hidden('item_key', $key) }} - {{ Form::hidden('qty', $item->qty) }} - {{ Form::text('item_discount', $item->item_discount, ['id' => 'item_discount-' . $key, 'style' => 'width:100px;text-align:right']) }} + {{ Form::hidden('item_discount', $item->item_discount) }} + {{ Form::number('qty', $item->qty, ['id' => 'qty-' . $key, 'style' => 'width:50px;text-align:center']) }} + {{ Form::submit('update-item-' . $key, ['style'=>'display:none']) }} {{ Form::close() }} - {{ formatRp($item->subtotal) }} - + {{ formatRp($item->subtotal) }} + {!! FormField::delete([ 'route' => ['cart.remove-draft-item', $draft->draftKey], 'onsubmit' => 'Yakin ingin menghapus Item ini?', + 'class' => '', ], 'x', ['id' => 'remove-item-' . $key, 'class' => 'btn btn-danger btn-xs'], ['item_index' => $key]) !!} @empty @endforelse + + + Subtotal : + {{ formatRp($draft->getSubtotal()) }} + + + + Diskon Total : + {{ formatRp($draft->getDiscountTotal()) }} + + + + Total : + {{ formatRp($draft->getTotal()) }} + + + @endif diff --git a/tests/Feature/TransactionEntryTest.php b/tests/Feature/TransactionEntryTest.php index 23aa487..2cdae85 100644 --- a/tests/Feature/TransactionEntryTest.php +++ b/tests/Feature/TransactionEntryTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature; use App\Cart\CartCollection; use App\Cart\CashDraft; use App\Cart\CreditDraft; +use App\Cart\Item; use App\Product; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\BrowserKitTestCase; @@ -66,7 +67,7 @@ class TransactionEntryTest extends BrowserKitTestCase $this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); // See product list appears $this->see($product->name); - $this->see($product->credit_price); + $this->see(formatRp($product->credit_price)); $this->seeElement('form', ['action' => route('cart.add-draft-item', [$draft->draftKey, $product->id])]); $this->seeElement('input', ['id' => 'qty-'.$product->id, 'name' => 'qty']); $this->seeElement('input', ['id' => 'add-product-'.$product->id]); @@ -74,9 +75,10 @@ class TransactionEntryTest extends BrowserKitTestCase } /** @test */ - public function user_can_add_item_to_draft() + public function user_can_add_item_to_cash_draft() { - $product = factory(Product::class)->create(['name' => 'Testing Produk 1', 'cash_price' => 400, 'credit_price' => 500]); + $product1 = factory(Product::class)->create(['name' => 'Testing Produk 1', 'cash_price' => 400, 'credit_price' => 500]); + $product2 = factory(Product::class)->create(['name' => 'Testing Produk 2', 'cash_price' => 1000, 'credit_price' => 1100]); $this->loginAsUser(); $cart = new CartCollection(); @@ -87,14 +89,81 @@ class TransactionEntryTest extends BrowserKitTestCase $this->visit(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); $this->type(2, 'qty'); - $this->press('add-product-'.$product->id); + $this->press('add-product-'.$product1->id); + $this->type(3, 'qty'); + $this->press('add-product-'.$product2->id); $this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); - $this->assertTrue($cart->draftHasItem($draft, $product)); - $this->assertEquals(800, $draft->getTotal()); + $this->assertTrue($cart->draftHasItem($draft, $product1)); + $this->assertTrue($cart->draftHasItem($draft, $product2)); + $this->assertEquals(3800, $draft->getTotal()); - $this->see(formatRp(800)); $this->seeElement('input', ['id' => 'qty-'. 0]); $this->seeElement('input', ['id' => 'item_discount-'. 0]); $this->seeElement('button', ['id' => 'remove-item-'. 0]); + $this->see(formatRp(3800)); + } + + /** @test */ + public function user_can_add_item_to_credit_draft() + { + $product1 = factory(Product::class)->create(['name' => 'Testing Produk 1', 'cash_price' => 400, 'credit_price' => 500]); + $product2 = factory(Product::class)->create(['name' => 'Testing Produk 2', 'cash_price' => 1000, 'credit_price' => 1100]); + $this->loginAsUser(); + + $cart = new CartCollection(); + $draft = new CreditDraft(); + $cart->add($draft); + + // Visit cart index with searched item + $this->visit(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); + + $this->type(2, 'qty'); + $this->press('add-product-'.$product1->id); + $this->type(3, 'qty'); + $this->press('add-product-'.$product2->id); + + $this->seePageIs(route('cart.show', [$draft->draftKey, 'query' => 'testing'])); + $this->assertTrue($cart->draftHasItem($draft, $product1)); + $this->assertTrue($cart->draftHasItem($draft, $product2)); + $this->assertEquals(4300, $draft->getTotal()); + } + + /** @test */ + public function user_can_update_item_qty() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, 1); + $item2 = new Item($product2, 3); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->loginAsUser(); + $this->visit(route('cart.show', $draft->draftKey)); + + $this->submitForm('update-item-0', [ + 'item_key' => 0, + 'qty' => 2, + 'item_discount' => 100, + ]); + + $this->submitForm('update-item-1', [ + 'item_key' => 1, + 'qty' => 2, + 'item_discount' => 100, + ]); + + $this->assertEquals(400, $draft->getDiscountTotal()); + $this->assertEquals(6000, $draft->getSubtotal()); + $this->assertEquals(5600, $draft->getTotal()); + + $this->see(formatRp($draft->getSubtotal())); + $this->see(formatRp($draft->getTotal())); } } diff --git a/tests/Unit/Integration/TransactionDraftTest.php b/tests/Unit/Integration/TransactionDraftTest.php index 4e4ff95..a0c8754 100644 --- a/tests/Unit/Integration/TransactionDraftTest.php +++ b/tests/Unit/Integration/TransactionDraftTest.php @@ -39,4 +39,104 @@ class TransactionDraftTest extends TestCase $this->assertEquals(2000, $draft->getTotal()); } + + /** @test */ + public function it_has_get_total_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + $count = 2; + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, $count); + $item2 = new Item($product2, $count); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertEquals(6000, $draft->getTotal()); + } + + /** @test */ + public function it_has_get_discount_total_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, 2); + $item2 = new Item($product2, 2); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertEquals(0, $draft->getDiscountTotal()); + } + + /** @test */ + public function it_has_get_total_item_qty_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, 1); + $item2 = new Item($product2, 3); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertEquals(4, $draft->getTotalQty()); + } + + /** @test */ + public function draft_item_has_set_item_discount_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, 1); + $item2 = new Item($product2, 3); + + $item2->setItemDiscount(100); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertEquals(6700, $draft->getTotal()); + } + + /** @test */ + public function it_has_get_subtotal_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $product2 = factory(Product::class)->create(['cash_price' => 2000]); + $item1 = new Item($product1, 1); + $item2 = new Item($product2, 3); + + $item2->setItemDiscount(100); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + $cart->addItemToDraft($draft->draftKey, $item2); + + $this->assertEquals(7000, $draft->getSubtotal()); + $this->assertEquals(6700, $draft->getTotal()); + } }