diff --git a/app/Services/InvoiceDraft/InvoiceDraft.php b/app/Services/InvoiceDraft/InvoiceDraft.php index 732ebd2..ee0554e 100644 --- a/app/Services/InvoiceDraft/InvoiceDraft.php +++ b/app/Services/InvoiceDraft/InvoiceDraft.php @@ -11,18 +11,58 @@ use App\Entities\Invoices\Invoice; */ class InvoiceDraft { + /** + * Invoice draft items. + * + * @var array + */ public $items = []; + /** + * Invoice data. + * + * @var string + */ public $date; + + /** + * Invoice notes. + * + * @var string + */ public $notes; + + /** + * Invoice due date. + * + * @var string + */ public $dueDate; + + /** + * Invoice project id from database. + * + * @var int + */ public $projectId; + /** + * Get item list sort by iten name. + * + * @return \Illuminate\Support\Collection + */ public function items() { 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) { $this->items[] = $item; @@ -30,26 +70,56 @@ class InvoiceDraft return $item; } + /** + * Remove item from the collection. + * + * @param int $itemKey Key of invoice item. + * + * @return void + */ public function removeItem($itemKey) { unset($this->items[$itemKey]); } + /** + * Empty out invoice draft items. + * + * @return void + */ public function empty() { $this->items = []; } + /** + * Get invoice total amount. + * + * @return int Total amount of invoice. + */ public function getTotal() { return $this->items()->sum('amount'); } + /** + * Get invoice items count. + * + * @return int Items count of invoice. + */ public function getItemsCount() { 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) { if (!isset($this->items[$itemKey])) { @@ -63,6 +133,11 @@ class InvoiceDraft return $item; } + /** + * Store invoice draft to database as invoice record. + * + * @return \App\Entities\Invoices\Invoice The saved invoice. + */ public function store() { $invoice = new Invoice(); @@ -81,6 +156,11 @@ class InvoiceDraft return $invoice; } + /** + * Get invoice items in array format. + * + * @return array Array of items. + */ protected function getItemsArray() { $items = []; @@ -94,6 +174,11 @@ class InvoiceDraft return $items; } + /** + * Destroy current invocie draft from the session. + * + * @return void + */ public function destroy() { $cart = app(InvoiceDraftCollection::class); diff --git a/app/Services/InvoiceDraft/InvoiceDraftCollection.php b/app/Services/InvoiceDraft/InvoiceDraftCollection.php index dac625d..9c619d3 100644 --- a/app/Services/InvoiceDraft/InvoiceDraftCollection.php +++ b/app/Services/InvoiceDraft/InvoiceDraftCollection.php @@ -11,15 +11,36 @@ use Illuminate\Support\Collection; */ class InvoiceDraftCollection { + /** + * Instance of invoice draft. + * + * @var string + */ private $instance; + + /** + * Laravel session. + * + * @var \Illuminate\Session\SessionManager + */ private $session; + /** + * Create new invoice draft instance. + */ public function __construct() { $this->session = session(); $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) { $instance = $instance ?: 'drafts'; @@ -29,11 +50,21 @@ class InvoiceDraftCollection return $this; } + /** + * Get instance name of current invoice draft. + * + * @return string + */ public function currentInstance() { return str_replace('invoices.', '', $this->instance); } + /** + * Add new invoice draft. + * + * @param \App\Services\InvoiceDrafts\InvoiceDraft $draft Invoice draft. + */ public function add(InvoiceDraft $draft) { $content = $this->getContent(); @@ -45,6 +76,13 @@ class InvoiceDraftCollection return $draft; } + /** + * Get an invoice draft. + * + * @param string $draftKey The invoice draft key. + * + * @return null|\App\Services\InvoiceDrafts\InvoiceDraft + */ public function get($draftKey) { $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) { $content = $this->getContent(); @@ -67,6 +113,13 @@ class InvoiceDraftCollection return $content[$draftKey]; } + /** + * Empty out an invoice draft items. + * + * @param string $draftKey Invoice draft key. + * + * @return void + */ public function emptyDraft($draftKey) { $content = $this->getContent(); @@ -74,6 +127,13 @@ class InvoiceDraftCollection $this->session->put($this->instance, $content); } + /** + * Remove an invocie draft. + * + * @param string $draftKey Invoice draft key. + * + * @return void + */ public function removeDraft($draftKey) { $content = $this->getContent(); @@ -81,11 +141,21 @@ class InvoiceDraftCollection $this->session->put($this->instance, $content); } + /** + * Get invoice draft collection content. + * + * @return \Illuminate\Support\Collection + */ public function content() { return $this->getContent(); } + /** + * Get invoice draft collection content. + * + * @return \Illuminate\Support\Collection + */ protected function getContent() { $content = $this->session->has($this->instance) ? $this->session->get($this->instance) : collect([]); @@ -93,16 +163,34 @@ class InvoiceDraftCollection return $content; } + /** + * Get invoice draft keys collection. + * + * @return \Illuminate\Support\Collection Collection of keys. + */ public function keys() { return $this->getContent()->keys(); } + /** + * Destroy an invoice draft. + * + * @return void + */ public function destroy() { $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) { $content = $this->getContent(); @@ -113,6 +201,15 @@ class InvoiceDraftCollection 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) { $content = $this->getContent(); @@ -121,6 +218,14 @@ class InvoiceDraftCollection $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) { $content = $this->getContent(); @@ -129,16 +234,31 @@ class InvoiceDraftCollection $this->session->put($this->instance, $content); } + /** + * Get invoice drafts count. + * + * @return int + */ public function count() { return $this->getContent()->count(); } + /** + * Check if current invoice draft is empty. + * + * @return boolean + */ public function isEmpty() { return $this->count() == 0; } + /** + * Check if current invoice draft has content. + * + * @return boolean + */ public function hasContent() { return !$this->isEmpty(); diff --git a/app/Services/InvoiceDraft/Item.php b/app/Services/InvoiceDraft/Item.php index a0c4fec..93c01d1 100644 --- a/app/Services/InvoiceDraft/Item.php +++ b/app/Services/InvoiceDraft/Item.php @@ -9,15 +9,38 @@ namespace App\Services\InvoiceDrafts; */ class Item { + /** + * Invoice item description. + * + * @var string + */ public $description; + + /** + * Invoice item amount. + * + * @var int + */ public $amount; + /** + * Create new invoice item. + * + * @param array $itemDetail Detail of an invoice item. + */ public function __construct(array $itemDetail) { $this->description = $itemDetail['description']; $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) { if (isset($newItemData['description'])) {