選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DocumentPreviewViewModel.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $number = match ($this->documentType) {
  65. DocumentType::Invoice => $this->document->invoice_number,
  66. DocumentType::RecurringInvoice => 'Auto-generated',
  67. DocumentType::Bill => $this->document->bill_number,
  68. DocumentType::Estimate => $this->document->estimate_number,
  69. };
  70. return [
  71. 'number' => $number,
  72. 'reference_number' => $this->document->order_number ?? $this->document->reference_number,
  73. 'date' => $this->document->date?->toDefaultDateFormat() ?? $this->document->calculateNextDate()?->toDefaultDateFormat(),
  74. 'due_date' => $this->document->due_date?->toDefaultDateFormat() ?? $this->document->expiration_date?->toDefaultDateFormat() ?? $this->document->calculateNextDueDate()?->toDefaultDateFormat(),
  75. 'currency_code' => $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency(),
  76. ];
  77. }
  78. private function getLineItems(): array
  79. {
  80. $currencyCode = $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency();
  81. return $this->document->lineItems->map(fn (DocumentLineItem $item) => [
  82. 'name' => $item->offering->name ?? '',
  83. 'description' => $item->description ?? '',
  84. 'quantity' => $item->quantity,
  85. 'unit_price' => CurrencyConverter::formatToMoney($item->unit_price, $currencyCode),
  86. 'subtotal' => CurrencyConverter::formatToMoney($item->subtotal, $currencyCode),
  87. ])->toArray();
  88. }
  89. private function getTotals(): array
  90. {
  91. $currencyCode = $this->document->currency_code ?? CurrencyAccessor::getDefaultCurrency();
  92. return [
  93. 'subtotal' => CurrencyConverter::formatToMoney($this->document->subtotal, $currencyCode),
  94. 'discount' => CurrencyConverter::formatToMoney($this->document->discount_total, $currencyCode),
  95. 'tax' => CurrencyConverter::formatToMoney($this->document->tax_total, $currencyCode),
  96. 'total' => CurrencyConverter::formatToMoney($this->document->total, $currencyCode),
  97. 'amount_due' => $this->document->amount_due ? CurrencyConverter::formatToMoney($this->document->amount_due, $currencyCode) : CurrencyConverter::formatToMoney($this->document->total, $currencyCode),
  98. ];
  99. }
  100. private function getStyle(): array
  101. {
  102. /** @var DocumentDefault $settings */
  103. $settings = $this->document->company->defaultInvoice;
  104. return [
  105. 'accent_color' => $settings->accent_color ?? '#000000',
  106. 'show_logo' => $settings->show_logo ?? false,
  107. ];
  108. }
  109. }