diff --git a/app/Cart/CartCollection.php b/app/Cart/CartCollection.php index ad93954..5763431 100644 --- a/app/Cart/CartCollection.php +++ b/app/Cart/CartCollection.php @@ -52,6 +52,19 @@ class CartCollection return null; } + public function updateDraftAttributes($draftKey, $draftAttributes) + { + $content = $this->getContent(); + + foreach ($draftAttributes as $attribute => $value) { + $content[$draftKey]->{$attribute} = $value; + } + + $this->session->put($this->instance, $content); + + return $content[$draftKey]; + } + public function removeDraft($draftKey) { $content = $this->getContent(); @@ -85,39 +98,28 @@ class CartCollection $this->session->remove($this->instance); } - public function addItemToCart($draftKey, $item) + public function addProductToDraft($draftKey, $item) { $content = $this->getContent(); - $content[$draftKey]->addItem($item); - $content[$draftKey] = $this->updateCart($content[$draftKey]); + $content[$draftKey]->addProduct($item); + $content[$draftKey] = $this->updateDraft($content[$draftKey]); $this->session->put($this->instance, $content); } - public function updateCart(Cart $draft) - { - $draft->charged_weight = $draft->getChargedWeight(); - $draft->items_count = $draft->itemsCount(); - $draft->pcs_count = $draft->itemsCount(); - $draft->base_charge = $draft->getCharge(); - $draft->subtotal = $draft->getCharge(); - $draft->total = $draft->getCharge(); - return $draft; - } - - public function updateCartItem($draftKey, $itemKey, $newItemData) + public function updateDraftProduct($draftKey, $itemKey, $newProductData) { $content = $this->getContent(); - $content[$draftKey]->updateItem($itemKey, $newItemData); - $content[$draftKey] = $this->updateCart($content[$draftKey]); + $content[$draftKey]->updateProduct($itemKey, $newProductData); + $content[$draftKey] = $this->updateDraft($content[$draftKey]); $this->session->put($this->instance, $content); } - public function removeItemFromCart($draftKey, $itemKey) + public function removeProductFromDraft($draftKey, $itemKey) { $content = $this->getContent(); - $content[$draftKey]->removeItem($itemKey); + $content[$draftKey]->removeProduct($itemKey); $this->session->put($this->instance, $content); } diff --git a/app/Cart/CashDraft.php b/app/Cart/CashDraft.php index 54d0f47..2d774f5 100644 --- a/app/Cart/CashDraft.php +++ b/app/Cart/CashDraft.php @@ -5,7 +5,7 @@ namespace App\Cart; /** * Cash Draft */ -class CashDraft implements TransactionDraft +class CashDraft extends TransactionDraft { public $draftKey; } \ No newline at end of file diff --git a/app/Cart/CreditDraft.php b/app/Cart/CreditDraft.php index d5876d2..7777af6 100644 --- a/app/Cart/CreditDraft.php +++ b/app/Cart/CreditDraft.php @@ -5,7 +5,7 @@ namespace App\Cart; /** * Credit Draft */ -class CreditDraft implements TransactionDraft +class CreditDraft extends TransactionDraft { public $draftKey; } \ No newline at end of file diff --git a/app/Cart/TransactionDraft.php b/app/Cart/TransactionDraft.php index caf364b..7d33cf0 100644 --- a/app/Cart/TransactionDraft.php +++ b/app/Cart/TransactionDraft.php @@ -5,4 +5,20 @@ namespace App\Cart; /** * Transaction Draft Interface */ -interface TransactionDraft {} \ No newline at end of file +abstract class TransactionDraft +{ + public function toArray() + { + return [ + 'invoice_no' => 2, + 'date' => 1, + 'items' => [], + 'total' => 0, + 'payment' => 0, + 'customer' => 0, + 'status_id' => 0, + 'creator_id' => 0, + 'remark' => '', + ]; + } +} \ No newline at end of file diff --git a/tests/Unit/CartCollectionTest.php b/tests/Unit/CartCollectionTest.php index e496c7e..5f22add 100644 --- a/tests/Unit/CartCollectionTest.php +++ b/tests/Unit/CartCollectionTest.php @@ -97,4 +97,52 @@ class CartCollectionTest extends TestCase $this->assertCount(0, $cart->content()); $this->assertTrue($cart->isEmpty()); } + + /** @test */ + public function it_has_content_keys() + { + $cart = new CartCollection; + + $cashDraft = new CashDraft; + $creditDraft = new CreditDraft; + + $cart->add($cashDraft); + $cart->add($creditDraft); + + $this->assertCount(2, $cart->keys()); + $cart->removeDraft($cart->content()->keys()->last()); + $this->assertCount(1, $cart->keys()); + } + + /** @test */ + public function it_can_update_a_draft_attributes() + { + $cart = new CartCollection; + + $draft = $cart->add(new CashDraft); + $this->assertCount(1, $cart->content()); + + $newDraftAttribute = [ + 'invoice_no' => 2, + 'date' => 1, + 'items' => [], + 'total' => 0, + 'payment' => 0, + 'customer' => 0, + 'status_id' => 0, + 'creator_id' => 0, + 'remark' => 0, + ]; + + $cart->updateDraftAttributes($draft->draftKey, $newDraftAttribute); + $this->assertArrayHasKey('invoice_no', $draft->toArray()); + $this->assertArrayHasKey('date', $draft->toArray()); + $this->assertArrayHasKey('items', $draft->toArray()); + $this->assertArrayHasKey('total', $draft->toArray()); + $this->assertArrayHasKey('payment', $draft->toArray()); + $this->assertArrayHasKey('customer', $draft->toArray()); + $this->assertArrayHasKey('status_id', $draft->toArray()); + $this->assertArrayHasKey('creator_id', $draft->toArray()); + $this->assertArrayHasKey('remark', $draft->toArray()); + } }