diff --git a/app/Helpers/Invoice/InvoiceHelper.php b/app/Helpers/Invoice/InvoiceHelper.php new file mode 100644 index 0000000000..c2ff320260 --- /dev/null +++ b/app/Helpers/Invoice/InvoiceHelper.php @@ -0,0 +1,13 @@ +invoice = $invoice; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 34aef65211..f37cb362a7 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -2,26 +2,45 @@ namespace App\Http\Controllers; +use App\Models\Invoice; use Illuminate\Http\Request; class InvoiceController extends BaseController { - public function __construct() + use MakesHash; + + protected $entityType = Invoice::class; + + protected $entityTransformer = InvoiceTransformer::class; + + /** + * @var ClientRepository + */ + protected $clientRepo; + + /** + * ClientController constructor. + * @param ClientRepository $clientRepo + */ + public function __construct(ClientRepository $clientRepo) { - parent::__construct(); + $this->clientRepo = $clientRepo; + } - + /** - * Display a listing of the resource. - * - * @return \Illuminate\Http\Response + * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View */ - public function index() + public function index(ClientFilters $filters) { - // + + $clients = Client::filter($filters); + + return $this->listResponse($clients); + } /** diff --git a/app/Transformers/InvoiceItemTransformer.php b/app/Transformers/InvoiceItemTransformer.php new file mode 100644 index 0000000000..434a53b249 --- /dev/null +++ b/app/Transformers/InvoiceItemTransformer.php @@ -0,0 +1,44 @@ + (int) $item->public_id, + 'product_key' => $item->product_key, + 'updated_at' => $item->updated_at, + 'archived_at' => $item->deleted_at, + 'notes' => $item->notes, + 'cost' => (float) $item->cost, + 'qty' => (float) ($item->qty ?: 0.0), + 'tax_name1' => $item->tax_name1 ? $item->tax_name1 : '', + 'tax_rate1' => (float) ($item->tax_rate1 ?: 0.0), + 'tax_name2' => $item->tax_name2 ? $item->tax_name2 : '', + 'tax_rate2' => (float) ($item->tax_rate2 ?: 0.0), + 'invoice_item_type_id' => (int) $item->invoice_item_type_id, + 'custom_value1' => $item->custom_value1 ?: '', + 'custom_value2' => $item->custom_value2 ?: '', + 'discount' => (float) $item->discount, + ]; + } +} diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php new file mode 100644 index 0000000000..8a6f208fac --- /dev/null +++ b/app/Transformers/InvoiceTransformer.php @@ -0,0 +1,173 @@ +client = $client; + } + + public function includeInvoiceItems(Invoice $invoice) + { + $transformer = new InvoiceItemTransformer($this->account, $this->serializer); + + return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEM); + } +/* + public function includeInvitations(Invoice $invoice) + { + $transformer = new InvitationTransformer($this->account, $this->serializer); + + return $this->includeCollection($invoice->invitations, $transformer, ENTITY_INVITATION); + } + + public function includePayments(Invoice $invoice) + { + $transformer = new PaymentTransformer($this->account, $this->serializer, $invoice); + + return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT); + } + + public function includeClient(Invoice $invoice) + { + $transformer = new ClientTransformer($this->account, $this->serializer); + + return $this->includeItem($invoice->client, $transformer, ENTITY_CLIENT); + } + + public function includeExpenses(Invoice $invoice) + { + $transformer = new ExpenseTransformer($this->account, $this->serializer); + + return $this->includeCollection($invoice->expenses, $transformer, ENTITY_EXPENSE); + } + + public function includeDocuments(Invoice $invoice) + { + $transformer = new DocumentTransformer($this->account, $this->serializer); + + $invoice->documents->each(function ($document) use ($invoice) { + $document->setRelation('invoice', $invoice); + }); + + return $this->includeCollection($invoice->documents, $transformer, ENTITY_DOCUMENT); + } +*/ + public function transform(Invoice $invoice) + { + return [ + 'id' => (int) $invoice->public_id, + 'amount' => (float) $invoice->amount, + 'balance' => (float) $invoice->balance, + 'client_id' => (int) ($this->client ? $this->client->public_id : $invoice->client->public_id), + 'invoice_status_id' => (int) ($invoice->invoice_status_id ?: 1), + 'updated_at' => $invoice->updated_at, + 'archived_at' => $invoice->deleted_at, + 'invoice_number' => $invoice->is_recurring ? '' : $invoice->invoice_number, + 'discount' => (float) $invoice->discount, + 'po_number' => $invoice->po_number, + 'invoice_date' => $invoice->invoice_date ?: '', + 'due_date' => $invoice->due_date ?: '', + 'terms' => $invoice->terms, + 'public_notes' => $invoice->public_notes ?: '', + 'private_notes' => $invoice->private_notes ?: '', + 'is_deleted' => (bool) $invoice->is_deleted, + 'invoice_type_id' => (int) $invoice->invoice_type_id, + 'is_recurring' => (bool) $invoice->is_recurring, + 'frequency_id' => (int) $invoice->frequency_id, + 'start_date' => $invoice->start_date ?: '', + 'end_date' => $invoice->end_date ?: '', + 'last_sent_date' => $invoice->last_sent_date ?: '', + 'recurring_invoice_id' => (int) ($invoice->recurring_invoice_id ?: 0), + 'tax_name1' => $invoice->tax_name1 ? $invoice->tax_name1 : '', + 'tax_rate1' => (float) $invoice->tax_rate1, + 'tax_name2' => $invoice->tax_name2 ? $invoice->tax_name2 : '', + 'tax_rate2' => (float) $invoice->tax_rate2, + 'is_amount_discount' => (bool) ($invoice->is_amount_discount ?: false), + 'invoice_footer' => $invoice->invoice_footer ?: '', + 'partial' => (float) ($invoice->partial ?: 0.0), + 'partial_due_date' => $invoice->partial_due_date ?: '', + 'has_tasks' => (bool) $invoice->has_tasks, + 'auto_bill' => (bool) $invoice->auto_bill, + 'custom_value1' => (float) $invoice->custom_value1, + 'custom_value2' => (float) $invoice->custom_value2, + 'custom_taxes1' => (bool) $invoice->custom_taxes1, + 'custom_taxes2' => (bool) $invoice->custom_taxes2, + 'has_expenses' => (bool) $invoice->has_expenses, + 'quote_invoice_id' => (int) ($invoice->quote_invoice_id ?: 0), + 'custom_text_value1' => $invoice->custom_text_value1 ?: '', + 'custom_text_value2' => $invoice->custom_text_value2 ?: '', + 'is_quote' => (bool) $invoice->isType(INVOICE_TYPE_QUOTE), // Temp to support mobile app + 'is_public' => (bool) $invoice->is_public, + 'filename' => $invoice->getFileName(), + 'options' => $invoice->options ?: '', + 'backup' => $invoice->backup ?: '', + ]; + } +} \ No newline at end of file