From bdad3cfde7d13f0ef5de5ea36d4b55576cb5fbff Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 2 May 2017 22:54:17 +0700 Subject: [PATCH] Product getPrice method returns cash_price attribute if credit_price is 0 or null --- app/Product.php | 3 +-- resources/views/cart/partials/product-search-box.blade.php | 2 +- .../views/cart/partials/product-search-result-box.blade.php | 2 +- tests/Unit/Integration/ProductTest.php | 10 ++++++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Product.php b/app/Product.php index b85d0e3..2e05749 100644 --- a/app/Product.php +++ b/app/Product.php @@ -10,8 +10,7 @@ class Product extends Model public function getPrice($type = 'cash') { - // TODO: if there is no credit_price then return cash_price - if ($type == 'credit') { + if ($type == 'credit' && $this->credit_price) { return $this->credit_price; } diff --git a/resources/views/cart/partials/product-search-box.blade.php b/resources/views/cart/partials/product-search-box.blade.php index 912bd7e..9d6c39e 100644 --- a/resources/views/cart/partials/product-search-box.blade.php +++ b/resources/views/cart/partials/product-search-box.blade.php @@ -4,7 +4,7 @@ - Refresh + {{ link_to_route('cart.show', 'Bersihkan Pencarian', [$draft->draftKey], ['class' => 'btn btn-sm']) }} @includeWhen ($queriedProducts, 'cart.partials.product-search-result-box') diff --git a/resources/views/cart/partials/product-search-result-box.blade.php b/resources/views/cart/partials/product-search-result-box.blade.php index e0754b0..6041773 100644 --- a/resources/views/cart/partials/product-search-result-box.blade.php +++ b/resources/views/cart/partials/product-search-result-box.blade.php @@ -13,7 +13,7 @@ {{ $product->name }} {{ $product->unit->name }} - {{ formatRp($draft->type == 'cash' ? $product->cash_price : $product->credit_price) }} + {{ formatRp($product->getPrice($draft->type)) }}
{{ csrf_field() }} diff --git a/tests/Unit/Integration/ProductTest.php b/tests/Unit/Integration/ProductTest.php index 0571930..6e0c06f 100644 --- a/tests/Unit/Integration/ProductTest.php +++ b/tests/Unit/Integration/ProductTest.php @@ -31,4 +31,14 @@ class ProductTest extends TestCase $this->assertEquals($product->credit_price, $product->getPrice('credit')); $this->assertEquals(3000, $product->getPrice('credit')); } + + /** @test */ + public function product_get_price_returns_cash_price_if_credit_price_is_0_or_null() + { + $product = new Product(['cash_price' => 2000, 'credit_price' => 0]); + $this->assertEquals(2000, $product->getPrice('credit')); + + $product = new Product(['cash_price' => 2000, 'credit_price' => null]); + $this->assertEquals(2000, $product->getPrice('credit')); + } }