Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DocumentPreviewViewModel.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\View\Models;
  3. use App\Enums\Accounting\DocumentType;
  4. use App\Models\Accounting\DocumentLineItem;
  5. use App\Models\Common\Client;
  6. use App\Models\Company;
  7. use App\Models\Setting\DocumentDefault;
  8. use App\Utilities\Currency\CurrencyAccessor;
  9. use App\Utilities\Currency\CurrencyConverter;
  10. use Illuminate\Database\Eloquent\Model;
  11. class DocumentPreviewViewModel
  12. {
  13. public function __construct(
  14. public Model $document,
  15. public DocumentType $documentType = DocumentType::Invoice,
  16. ) {}
  17. public function buildViewData(): array
  18. {
  19. return [
  20. 'company' => $this->getCompanyDetails(),
  21. 'client' => $this->getClientDetails(),
  22. 'metadata' => $this->getDocumentMetadata(),
  23. 'lineItems' => $this->getLineItems(),
  24. 'totals' => $this->getTotals(),
  25. 'header' => $this->document->header,
  26. 'footer' => $this->document->footer,
  27. 'terms' => $this->document->terms,
  28. 'logo' => $this->document->logo,
  29. 'style' => $this->getStyle(),
  30. 'labels' => $this->documentType->getLabels(),
  31. ];
  32. }
  33. private function getCompanyDetails(): array
  34. {
  35. /** @var Company $company */
  36. $company = $this->document->company;
  37. $profile = $company->profile;
  38. return [
  39. 'name' => $company->name,
  40. 'address' => $profile->address ?? '',
  41. 'city' => $profile->city?->name ?? '',
  42. 'state' => $profile->state?->name ?? '',
  43. 'zip_code' => $profile->zip_code ?? '',
  44. 'country' => $profile->state?->country->name ?? '',
  45. ];
  46. }
  47. private function getClientDetails(): array
  48. {
  49. /** @var Client $client */
  50. $client = $this->document->client;
  51. $address = $client->billingAddress ?? null;
  52. return [
  53. 'name' => $client->name,
  54. 'address_line_1' => $address->address_line_1 ?? '',
  55. 'address_line_2' => $address->address_line_2 ?? '',
  56. 'city' => $address->city ?? '',
  57. 'state' => $address->state ?? '',
  58. 'postal_code' => $address->postal_code ?? '',
  59. 'country' => $address->country ?? '',
  60. ];
  61. }
  62. private function getDocumentMetadata(): array
  63. {
  64. return [
  65. 'number' => $this->document->invoice_number ?? $this->document->estimate_number,
  66. 'reference_number' => $this->document->order_number ?? $this->document->reference_number,
  67. 'date' => $this->document->date?->toDefaultDateFormat(),
  68. 'due_date' => $this->document->due_date?->toDefaultDateFormat() ?? $this->document->expiration_date?->toDefaultDateFormat(),
  69. 'currency_code' => $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency(),
  70. ];
  71. }
  72. private function getLineItems(): array
  73. {
  74. $currencyCode = $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency();
  75. return $this->document->lineItems->map(fn (DocumentLineItem $item) => [
  76. 'name' => $item->offering->name ?? '',
  77. 'description' => $item->description ?? '',
  78. 'quantity' => $item->quantity,
  79. 'unit_price' => CurrencyConverter::formatToMoney($item->unit_price, $currencyCode),
  80. 'subtotal' => CurrencyConverter::formatToMoney($item->subtotal, $currencyCode),
  81. ])->toArray();
  82. }
  83. private function getTotals(): array
  84. {
  85. $currencyCode = $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency();
  86. return [
  87. 'subtotal' => CurrencyConverter::formatToMoney($this->document->subtotal, $currencyCode),
  88. 'discount' => CurrencyConverter::formatToMoney($this->document->discount_total, $currencyCode),
  89. 'tax' => CurrencyConverter::formatToMoney($this->document->tax_total, $currencyCode),
  90. 'total' => CurrencyConverter::formatToMoney($this->document->total, $currencyCode),
  91. 'amount_due' => $this->document->amount_due ? CurrencyConverter::formatToMoney($this->document->amount_due, $currencyCode) : null,
  92. ];
  93. }
  94. private function getStyle(): array
  95. {
  96. /** @var DocumentDefault $settings */
  97. $settings = $this->document->company->defaultInvoice;
  98. return [
  99. 'accent_color' => $settings->accent_color ?? '#000000',
  100. 'show_logo' => $settings->show_logo ?? false,
  101. ];
  102. }
  103. }