diff --git a/app/Cart/CartCollection.php b/app/Cart/CartCollection.php index 304367f..15f783a 100644 --- a/app/Cart/CartCollection.php +++ b/app/Cart/CartCollection.php @@ -111,7 +111,15 @@ class CartCollection $item->subtotal = $item->product->getPrice('credit') * $item->qty; } - $content[$draftKey]->addItem($item); + $foundItem = $draft->search($item->product); + + if (!is_null($foundItem)) { + $itemKey = $draft->searchItemKeyFor($item->product); + $content[$draftKey]->updateItem($itemKey, ['qty' => $foundItem->qty + $item->qty]); + } else { + $content[$draftKey]->addItem($item); + } + $this->session->put($this->instance, $content); diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index e2be1a5..5b07a19 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -98,6 +98,13 @@ abstract class TransactionDraft return $productItem; } + public function searchItemKeyFor(Product $product) + { + return $this->items()->search(function($item, $key) use ($product) { + return $item->product->id == $product->id; + }); + } + public function getExchange() { return $this->payment - $this->getTotal(); diff --git a/app/Product.php b/app/Product.php index 076e9c7..22f716c 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['name', 'cash_price', 'credit_price', 'unit_id']; + protected $fillable = ['id', 'name', 'cash_price', 'credit_price', 'unit_id']; public function getPrice($type = 'cash') { diff --git a/tests/Unit/CartCollectionTest.php b/tests/Unit/CartCollectionTest.php index 2d596c4..18356ea 100644 --- a/tests/Unit/CartCollectionTest.php +++ b/tests/Unit/CartCollectionTest.php @@ -159,6 +159,30 @@ class CartCollectionTest extends TestCase $cart->addItemToDraft($draft->draftKey, $item); $this->assertEquals(2000, $draft->getTotal()); + $this->assertEquals(1, $draft->getItemsCount()); + $this->assertEquals(2, $draft->getTotalQty()); + } + + /** @test */ + public function it_adds_product_qty_to_draft_if_product_id_exists() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + $count = 2; + + $item = new Item(new Product(['id' => 1, 'cash_price' => 1000]), $count); + $cart->addItemToDraft($draft->draftKey, $item); + + $item = new Item(new Product(['id' => 1, 'cash_price' => 1000]), $count); + $cart->addItemToDraft($draft->draftKey, $item); + + $item = new Item(new Product(['id' => 2, 'cash_price' => 1000]), $count); + $cart->addItemToDraft($draft->draftKey, $item); + + $this->assertEquals(6000, $draft->getTotal()); + $this->assertEquals(2, $draft->getItemsCount()); + $this->assertEquals(6, $draft->getTotalQty()); } /** @test */ diff --git a/tests/Unit/Integration/TransactionDraftTest.php b/tests/Unit/Integration/TransactionDraftTest.php index 158a411..5eb9f47 100644 --- a/tests/Unit/Integration/TransactionDraftTest.php +++ b/tests/Unit/Integration/TransactionDraftTest.php @@ -158,8 +158,8 @@ class TransactionDraftTest extends TestCase $draft = $cart->add(new CashDraft()); - $product1 = factory(Product::class)->make(['cash_price' => 1000]); - $product2 = factory(Product::class)->make(['cash_price' => 2000]); + $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 @@ -223,4 +223,45 @@ class TransactionDraftTest extends TestCase 'user_id' => 1, ]); } + + /** @test */ + public function it_has_product_search_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + $count = 2; + $product1 = factory(Product::class)->create(['cash_price' => 1000]); + $item1 = new Item($product1, $count); + + // Add items to draft + $cart->addItemToDraft($draft->draftKey, $item1); + + $this->assertEquals($draft->search($product1)->id, $product1->id); + } + + /** @test */ + public function it_has_search_item_key_for_product_method() + { + $cart = new CartCollection(); + + $draft = $cart->add(new CashDraft()); + $count = 2; + + $product1 = factory(Product::class)->create(); + $item1 = new Item($product1, $count); + $cart->addItemToDraft($draft->draftKey, $item1); + + $product2 = factory(Product::class)->create(); + $item2 = new Item($product2, $count); + $cart->addItemToDraft($draft->draftKey, $item2); + + $product3 = factory(Product::class)->create(); + $item3 = new Item($product3, $count); + $cart->addItemToDraft($draft->draftKey, $item3); + + $this->assertEquals($draft->searchItemKeyFor($product3), 2); + $this->assertEquals($draft->searchItemKeyFor($product2), 1); + $this->assertEquals($draft->searchItemKeyFor($product1), 0); + } }