Browse Source

Adds product qty to transaction draft if product id exists

pull/6/head
Nafies Luthfi 9 years ago
parent
commit
df1872efad
  1. 10
      app/Cart/CartCollection.php
  2. 7
      app/Cart/TransactionDraft.php
  3. 2
      app/Product.php
  4. 24
      tests/Unit/CartCollectionTest.php
  5. 45
      tests/Unit/Integration/TransactionDraftTest.php

10
app/Cart/CartCollection.php

@ -111,7 +111,15 @@ class CartCollection
$item->subtotal = $item->product->getPrice('credit') * $item->qty; $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); $this->session->put($this->instance, $content);

7
app/Cart/TransactionDraft.php

@ -98,6 +98,13 @@ abstract class TransactionDraft
return $productItem; 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() public function getExchange()
{ {
return $this->payment - $this->getTotal(); return $this->payment - $this->getTotal();

2
app/Product.php

@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Product extends 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') public function getPrice($type = 'cash')
{ {

24
tests/Unit/CartCollectionTest.php

@ -159,6 +159,30 @@ class CartCollectionTest extends TestCase
$cart->addItemToDraft($draft->draftKey, $item); $cart->addItemToDraft($draft->draftKey, $item);
$this->assertEquals(2000, $draft->getTotal()); $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 */ /** @test */

45
tests/Unit/Integration/TransactionDraftTest.php

@ -158,8 +158,8 @@ class TransactionDraftTest extends TestCase
$draft = $cart->add(new CashDraft()); $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); $item1 = new Item($product1, 1);
$item2 = new Item($product2, 3); $item2 = new Item($product2, 3);
// Add items to draft // Add items to draft
@ -223,4 +223,45 @@ class TransactionDraftTest extends TestCase
'user_id' => 1, '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);
}
} }
Loading…
Cancel
Save