From 2f9f7b6d36fb1efca73557b7b71a2f0fdf38caea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Sat, 2 Jan 2021 12:21:16 +0300 Subject: [PATCH] added item taxes and totals to document api --- app/Models/Document/DocumentItemTax.php | 1 - app/Transformers/Document/Document.php | 26 ++++++++-- ...umentHistories.php => DocumentHistory.php} | 2 +- .../{DocumentItems.php => DocumentItem.php} | 2 +- app/Transformers/Document/DocumentItemTax.php | 48 +++++++++++++++++++ .../{DocumentTotals.php => DocumentTotal.php} | 2 +- 6 files changed, 73 insertions(+), 8 deletions(-) rename app/Transformers/Document/{DocumentHistories.php => DocumentHistory.php} (93%) rename app/Transformers/Document/{DocumentItems.php => DocumentItem.php} (94%) create mode 100644 app/Transformers/Document/DocumentItemTax.php rename app/Transformers/Document/{DocumentTotals.php => DocumentTotal.php} (94%) diff --git a/app/Models/Document/DocumentItemTax.php b/app/Models/Document/DocumentItemTax.php index ddcaf7956..b927e8f07 100644 --- a/app/Models/Document/DocumentItemTax.php +++ b/app/Models/Document/DocumentItemTax.php @@ -5,7 +5,6 @@ namespace App\Models\Document; use App\Abstracts\Model; use App\Traits\Currencies; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\BelongsTo; use Znck\Eloquent\Traits\BelongsToThrough; class DocumentItemTax extends Model diff --git a/app/Transformers/Document/Document.php b/app/Transformers/Document/Document.php index cdc1dfc23..6a51ef7bf 100644 --- a/app/Transformers/Document/Document.php +++ b/app/Transformers/Document/Document.php @@ -2,10 +2,10 @@ namespace App\Transformers\Document; +use App\Models\Document\Document as Model; use App\Transformers\Banking\Transaction; use App\Transformers\Common\Contact; use App\Transformers\Setting\Currency; -use App\Models\Document\Document as Model; use League\Fractal\TransformerAbstract; class Document extends TransformerAbstract @@ -13,7 +13,7 @@ class Document extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['contact', 'currency', 'histories', 'items', 'transactions']; + protected $defaultIncludes = ['contact', 'currency', 'histories', 'items', 'item_taxes', 'totals', 'transactions']; /** * @param Model $model @@ -70,7 +70,7 @@ class Document extends TransformerAbstract */ public function includeHistories(Model $model) { - return $this->collection($model->histories, new DocumentHistories()); + return $this->collection($model->histories, new DocumentHistory()); } /** @@ -79,7 +79,25 @@ class Document extends TransformerAbstract */ public function includeItems(Model $model) { - return $this->collection($model->items, new DocumentItems()); + return $this->collection($model->items, new DocumentItem()); + } + + /** + * @param Model $model + * @return \League\Fractal\Resource\Collection + */ + public function includeItemTaxes(Model $model) + { + return $this->collection($model->item_taxes, new DocumentItemTax()); + } + + /** + * @param Model $model + * @return \League\Fractal\Resource\Collection + */ + public function includeTotals(Model $model) + { + return $this->collection($model->totals, new DocumentTotal()); } /** diff --git a/app/Transformers/Document/DocumentHistories.php b/app/Transformers/Document/DocumentHistory.php similarity index 93% rename from app/Transformers/Document/DocumentHistories.php rename to app/Transformers/Document/DocumentHistory.php index 007d37061..dd486f7e5 100644 --- a/app/Transformers/Document/DocumentHistories.php +++ b/app/Transformers/Document/DocumentHistory.php @@ -5,7 +5,7 @@ namespace App\Transformers\Document; use App\Models\Document\DocumentHistory as Model; use League\Fractal\TransformerAbstract; -class DocumentHistories extends TransformerAbstract +class DocumentHistory extends TransformerAbstract { /** * @param Model $model diff --git a/app/Transformers/Document/DocumentItems.php b/app/Transformers/Document/DocumentItem.php similarity index 94% rename from app/Transformers/Document/DocumentItems.php rename to app/Transformers/Document/DocumentItem.php index afe1df15b..300a1dfb3 100644 --- a/app/Transformers/Document/DocumentItems.php +++ b/app/Transformers/Document/DocumentItem.php @@ -5,7 +5,7 @@ namespace App\Transformers\Document; use App\Models\Document\DocumentItem as Model; use League\Fractal\TransformerAbstract; -class DocumentItems extends TransformerAbstract +class DocumentItem extends TransformerAbstract { /** * @param Model $model diff --git a/app/Transformers/Document/DocumentItemTax.php b/app/Transformers/Document/DocumentItemTax.php new file mode 100644 index 000000000..67e5584aa --- /dev/null +++ b/app/Transformers/Document/DocumentItemTax.php @@ -0,0 +1,48 @@ + $model->id, + 'company_id' => $model->company_id, + 'type' => $model->type, + 'document_id' => $model->document_id, + 'document_item_id' => $model->document_item_id, + 'tax_id' => $model->tax_id, + 'name' => $model->name, + 'amount' => $model->amount, + 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', + 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', + ]; + } + + /** + * @param Model $model + * @return mixed + */ + public function includeTax(Model $model) + { + if (!$model->tax) { + return $this->null(); + } + + return $this->item($model->tax, new Tax()); + } +} diff --git a/app/Transformers/Document/DocumentTotals.php b/app/Transformers/Document/DocumentTotal.php similarity index 94% rename from app/Transformers/Document/DocumentTotals.php rename to app/Transformers/Document/DocumentTotal.php index 19336280d..3fec489d2 100644 --- a/app/Transformers/Document/DocumentTotals.php +++ b/app/Transformers/Document/DocumentTotal.php @@ -5,7 +5,7 @@ namespace App\Transformers\Document; use App\Models\Document\DocumentTotal as Model; use League\Fractal\TransformerAbstract; -class DocumentTotals extends TransformerAbstract +class DocumentTotal extends TransformerAbstract { /**