Browse Source

Update invoice service classes docblocks

pull/6/head
Nafies Luthfi 8 years ago
parent
commit
07aa860f71
  1. 85
      app/Services/InvoiceDraft/InvoiceDraft.php
  2. 120
      app/Services/InvoiceDraft/InvoiceDraftCollection.php
  3. 23
      app/Services/InvoiceDraft/Item.php

85
app/Services/InvoiceDraft/InvoiceDraft.php

@ -11,18 +11,58 @@ use App\Entities\Invoices\Invoice;
*/ */
class InvoiceDraft class InvoiceDraft
{ {
/**
* Invoice draft items.
*
* @var array
*/
public $items = []; public $items = [];
/**
* Invoice data.
*
* @var string
*/
public $date; public $date;
/**
* Invoice notes.
*
* @var string
*/
public $notes; public $notes;
/**
* Invoice due date.
*
* @var string
*/
public $dueDate; public $dueDate;
/**
* Invoice project id from database.
*
* @var int
*/
public $projectId; public $projectId;
/**
* Get item list sort by iten name.
*
* @return \Illuminate\Support\Collection
*/
public function items() public function items()
{ {
return collect($this->items)->sortBy('name'); return collect($this->items)->sortBy('name');
} }
/**
* Add new item to invoice item collection.
*
* @param \App\Services\InvoiceDrafts\Item $item
*
* @return \App\Services\InvoiceDrafts\Item
*/
public function addItem(Item $item) public function addItem(Item $item)
{ {
$this->items[] = $item; $this->items[] = $item;
@ -30,26 +70,56 @@ class InvoiceDraft
return $item; return $item;
} }
/**
* Remove item from the collection.
*
* @param int $itemKey Key of invoice item.
*
* @return void
*/
public function removeItem($itemKey) public function removeItem($itemKey)
{ {
unset($this->items[$itemKey]); unset($this->items[$itemKey]);
} }
/**
* Empty out invoice draft items.
*
* @return void
*/
public function empty() public function empty()
{ {
$this->items = []; $this->items = [];
} }
/**
* Get invoice total amount.
*
* @return int Total amount of invoice.
*/
public function getTotal() public function getTotal()
{ {
return $this->items()->sum('amount'); return $this->items()->sum('amount');
} }
/**
* Get invoice items count.
*
* @return int Items count of invoice.
*/
public function getItemsCount() public function getItemsCount()
{ {
return $this->items()->count(); return $this->items()->count();
} }
/**
* Update an invoice item.
*
* @param int $itemKey The item key
* @param array $newItemData The item attributes.
*
* @return null|\App\Services\InvoiceDrafts\Item
*/
public function updateItem($itemKey, $newItemData) public function updateItem($itemKey, $newItemData)
{ {
if (!isset($this->items[$itemKey])) { if (!isset($this->items[$itemKey])) {
@ -63,6 +133,11 @@ class InvoiceDraft
return $item; return $item;
} }
/**
* Store invoice draft to database as invoice record.
*
* @return \App\Entities\Invoices\Invoice The saved invoice.
*/
public function store() public function store()
{ {
$invoice = new Invoice(); $invoice = new Invoice();
@ -81,6 +156,11 @@ class InvoiceDraft
return $invoice; return $invoice;
} }
/**
* Get invoice items in array format.
*
* @return array Array of items.
*/
protected function getItemsArray() protected function getItemsArray()
{ {
$items = []; $items = [];
@ -94,6 +174,11 @@ class InvoiceDraft
return $items; return $items;
} }
/**
* Destroy current invocie draft from the session.
*
* @return void
*/
public function destroy() public function destroy()
{ {
$cart = app(InvoiceDraftCollection::class); $cart = app(InvoiceDraftCollection::class);

120
app/Services/InvoiceDraft/InvoiceDraftCollection.php

@ -11,15 +11,36 @@ use Illuminate\Support\Collection;
*/ */
class InvoiceDraftCollection class InvoiceDraftCollection
{ {
/**
* Instance of invoice draft.
*
* @var string
*/
private $instance; private $instance;
/**
* Laravel session.
*
* @var \Illuminate\Session\SessionManager
*/
private $session; private $session;
/**
* Create new invoice draft instance.
*/
public function __construct() public function __construct()
{ {
$this->session = session(); $this->session = session();
$this->instance('drafts'); $this->instance('drafts');
} }
/**
* Set new instance name of invoice draft.
*
* @param string $instance Invoice draft instance name.
*
* @return \App\Services\InvoiceDrafts\InvoiceDraft
*/
public function instance($instance = null) public function instance($instance = null)
{ {
$instance = $instance ?: 'drafts'; $instance = $instance ?: 'drafts';
@ -29,11 +50,21 @@ class InvoiceDraftCollection
return $this; return $this;
} }
/**
* Get instance name of current invoice draft.
*
* @return string
*/
public function currentInstance() public function currentInstance()
{ {
return str_replace('invoices.', '', $this->instance); return str_replace('invoices.', '', $this->instance);
} }
/**
* Add new invoice draft.
*
* @param \App\Services\InvoiceDrafts\InvoiceDraft $draft Invoice draft.
*/
public function add(InvoiceDraft $draft) public function add(InvoiceDraft $draft)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -45,6 +76,13 @@ class InvoiceDraftCollection
return $draft; return $draft;
} }
/**
* Get an invoice draft.
*
* @param string $draftKey The invoice draft key.
*
* @return null|\App\Services\InvoiceDrafts\InvoiceDraft
*/
public function get($draftKey) public function get($draftKey)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -53,6 +91,14 @@ class InvoiceDraftCollection
} }
} }
/**
* Update invoice draft attribute.
*
* @param string $draftKey Invoice draft key.
* @param array $draftAttributes Invoice draft attribute to be updated.
*
* @return \App\Services\InvoiceDrafts\InvoiceDraft
*/
public function updateDraftAttributes($draftKey, $draftAttributes) public function updateDraftAttributes($draftKey, $draftAttributes)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -67,6 +113,13 @@ class InvoiceDraftCollection
return $content[$draftKey]; return $content[$draftKey];
} }
/**
* Empty out an invoice draft items.
*
* @param string $draftKey Invoice draft key.
*
* @return void
*/
public function emptyDraft($draftKey) public function emptyDraft($draftKey)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -74,6 +127,13 @@ class InvoiceDraftCollection
$this->session->put($this->instance, $content); $this->session->put($this->instance, $content);
} }
/**
* Remove an invocie draft.
*
* @param string $draftKey Invoice draft key.
*
* @return void
*/
public function removeDraft($draftKey) public function removeDraft($draftKey)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -81,11 +141,21 @@ class InvoiceDraftCollection
$this->session->put($this->instance, $content); $this->session->put($this->instance, $content);
} }
/**
* Get invoice draft collection content.
*
* @return \Illuminate\Support\Collection
*/
public function content() public function content()
{ {
return $this->getContent(); return $this->getContent();
} }
/**
* Get invoice draft collection content.
*
* @return \Illuminate\Support\Collection
*/
protected function getContent() protected function getContent()
{ {
$content = $this->session->has($this->instance) ? $this->session->get($this->instance) : collect([]); $content = $this->session->has($this->instance) ? $this->session->get($this->instance) : collect([]);
@ -93,16 +163,34 @@ class InvoiceDraftCollection
return $content; return $content;
} }
/**
* Get invoice draft keys collection.
*
* @return \Illuminate\Support\Collection Collection of keys.
*/
public function keys() public function keys()
{ {
return $this->getContent()->keys(); return $this->getContent()->keys();
} }
/**
* Destroy an invoice draft.
*
* @return void
*/
public function destroy() public function destroy()
{ {
$this->session->remove($this->instance); $this->session->remove($this->instance);
} }
/**
* Add an item to an invoice draft.
*
* @param string $draftKey Invoice draft key.
* @param \App\Services\InvoiceDrafts\Item $item Invoice item.
*
* @return \App\Services\InvoiceDrafts\Item.
*/
public function addItemToDraft($draftKey, Item $item) public function addItemToDraft($draftKey, Item $item)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -113,6 +201,15 @@ class InvoiceDraftCollection
return $item; return $item;
} }
/**
* Update invoice draft item attributes.
*
* @param string $draftKey Invoice draft key.
* @param string $itemKey Invoice item key.
* @param array $newItemData Array of item attribute.
*
* @return void
*/
public function updateDraftItem($draftKey, $itemKey, $newItemData) public function updateDraftItem($draftKey, $itemKey, $newItemData)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -121,6 +218,14 @@ class InvoiceDraftCollection
$this->session->put($this->instance, $content); $this->session->put($this->instance, $content);
} }
/**
* Remove an invoice draft item.
*
* @param string $draftKey Invoice draft key.
* @param string $itemKey Invoice item key.
*
* @return void
*/
public function removeItemFromDraft($draftKey, $itemKey) public function removeItemFromDraft($draftKey, $itemKey)
{ {
$content = $this->getContent(); $content = $this->getContent();
@ -129,16 +234,31 @@ class InvoiceDraftCollection
$this->session->put($this->instance, $content); $this->session->put($this->instance, $content);
} }
/**
* Get invoice drafts count.
*
* @return int
*/
public function count() public function count()
{ {
return $this->getContent()->count(); return $this->getContent()->count();
} }
/**
* Check if current invoice draft is empty.
*
* @return boolean
*/
public function isEmpty() public function isEmpty()
{ {
return $this->count() == 0; return $this->count() == 0;
} }
/**
* Check if current invoice draft has content.
*
* @return boolean
*/
public function hasContent() public function hasContent()
{ {
return !$this->isEmpty(); return !$this->isEmpty();

23
app/Services/InvoiceDraft/Item.php

@ -9,15 +9,38 @@ namespace App\Services\InvoiceDrafts;
*/ */
class Item class Item
{ {
/**
* Invoice item description.
*
* @var string
*/
public $description; public $description;
/**
* Invoice item amount.
*
* @var int
*/
public $amount; public $amount;
/**
* Create new invoice item.
*
* @param array $itemDetail Detail of an invoice item.
*/
public function __construct(array $itemDetail) public function __construct(array $itemDetail)
{ {
$this->description = $itemDetail['description']; $this->description = $itemDetail['description'];
$this->amount = $itemDetail['amount']; $this->amount = $itemDetail['amount'];
} }
/**
* Update attribute of an invoice item.
*
* @param array $newItemData Item attribute that will be replaced.
*
* @return \App\Services\InvoiceDrafts\Item
*/
public function updateAttribute(array $newItemData) public function updateAttribute(array $newItemData)
{ {
if (isset($newItemData['description'])) { if (isset($newItemData['description'])) {

Loading…
Cancel
Save