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

InvoiceViewModel.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\View\Models;
  3. use App\Enums\Setting\Font;
  4. use App\Enums\Setting\PaymentTerms;
  5. use App\Models\Setting\DocumentDefault;
  6. use Filament\Panel\Concerns\HasFont;
  7. class InvoiceViewModel
  8. {
  9. use HasFont;
  10. public function __construct(
  11. public DocumentDefault $invoice,
  12. public ?array $data = null
  13. ) {
  14. }
  15. public function logo(): ?string
  16. {
  17. return $this->invoice->logo_url;
  18. }
  19. public function show_logo(): bool
  20. {
  21. return $this->data['show_logo'] ?? $this->invoice->show_logo ?? false;
  22. }
  23. // Company related methods
  24. public function company_name(): string
  25. {
  26. return $this->invoice->company->name;
  27. }
  28. public function company_address(): ?string
  29. {
  30. return $this->invoice->company->profile->address ?? null;
  31. }
  32. public function company_phone(): ?string
  33. {
  34. return $this->invoice->company->profile->phone_number ?? null;
  35. }
  36. public function company_city(): ?string
  37. {
  38. return $this->invoice->company->profile->city->name ?? null;
  39. }
  40. public function company_state(): ?string
  41. {
  42. return $this->invoice->company->profile->state->name ?? null;
  43. }
  44. public function company_zip(): ?string
  45. {
  46. return $this->invoice->company->profile->zip_code ?? null;
  47. }
  48. public function company_country(): ?string
  49. {
  50. return $this->invoice->company->profile->state->country->name ?? null;
  51. }
  52. // Invoice numbering related methods
  53. public function number_prefix(): string
  54. {
  55. return $this->data['number_prefix'] ?? $this->invoice->number_prefix ?? 'INV-';
  56. }
  57. public function number_digits(): int
  58. {
  59. return $this->data['number_digits'] ?? $this->invoice->number_digits ?? 5;
  60. }
  61. public function number_next(): string
  62. {
  63. return $this->data['number_next'] ?? $this->invoice->number_next;
  64. }
  65. public function invoice_number(): string
  66. {
  67. return DocumentDefault::getNumberNext(padded: true, format: true, prefix: $this->number_prefix(), digits: $this->number_digits(), next: $this->number_next());
  68. }
  69. // Invoice date related methods
  70. public function invoice_date(): string
  71. {
  72. return $this->invoice->company->locale->date_format->getLabel();
  73. }
  74. public function payment_terms(): string
  75. {
  76. return $this->data['payment_terms'] ?? $this->invoice->payment_terms?->value ?? PaymentTerms::DEFAULT;
  77. }
  78. public function invoice_due_date(): string
  79. {
  80. $dateFormat = $this->invoice->company->locale->date_format->value;
  81. return PaymentTerms::from($this->payment_terms())->getDueDate($dateFormat);
  82. }
  83. // Invoice header related methods
  84. public function header(): string
  85. {
  86. return $this->data['header'] ?? $this->invoice->header ?? 'Invoice';
  87. }
  88. public function subheader(): ?string
  89. {
  90. return $this->data['subheader'] ?? $this->invoice->subheader ?? null;
  91. }
  92. // Invoice styling
  93. public function accent_color(): string
  94. {
  95. return $this->data['accent_color'] ?? $this->invoice->accent_color;
  96. }
  97. public function fontFamily(): string
  98. {
  99. if ($this->data['font']) {
  100. return Font::from($this->data['font'])->getLabel();
  101. }
  102. if ($this->invoice->font) {
  103. return $this->invoice->font->getLabel();
  104. }
  105. return Font::from(Font::DEFAULT)->getLabel();
  106. }
  107. public function footer(): ?string
  108. {
  109. return $this->data['footer'] ?? $this->invoice->footer ?? null;
  110. }
  111. public function terms(): ?string
  112. {
  113. return $this->data['terms'] ?? $this->invoice->terms ?? null;
  114. }
  115. public function getItemColumnName(string $column, string $default): string
  116. {
  117. $custom = $this->data[$column]['custom'] ?? $this->invoice->{$column . '_custom'} ?? null;
  118. if ($custom) {
  119. return $custom;
  120. }
  121. $option = $this->data[$column]['option'] ?? $this->invoice->{$column . '_option'} ?? null;
  122. return $option ? $this->invoice->getLabelOptionFor($column, $option) : translate($default);
  123. }
  124. // Invoice column related methods
  125. public function item_name(): string
  126. {
  127. return $this->getItemColumnName('item_name', 'Items');
  128. }
  129. public function unit_name(): string
  130. {
  131. return $this->getItemColumnName('unit_name', 'Quantity');
  132. }
  133. public function price_name(): string
  134. {
  135. return $this->getItemColumnName('price_name', 'Price');
  136. }
  137. public function amount_name(): string
  138. {
  139. return $this->getItemColumnName('amount_name', 'Amount');
  140. }
  141. public function buildViewData(): array
  142. {
  143. return [
  144. 'logo' => $this->logo(),
  145. 'show_logo' => $this->show_logo(),
  146. 'company_name' => $this->company_name(),
  147. 'company_address' => $this->company_address(),
  148. 'company_phone' => $this->company_phone(),
  149. 'company_city' => $this->company_city(),
  150. 'company_state' => $this->company_state(),
  151. 'company_zip' => $this->company_zip(),
  152. 'company_country' => $this->company_country(),
  153. 'number_prefix' => $this->number_prefix(),
  154. 'number_digits' => $this->number_digits(),
  155. 'number_next' => $this->number_next(),
  156. 'invoice_number' => $this->invoice_number(),
  157. 'invoice_date' => $this->invoice_date(),
  158. 'invoice_due_date' => $this->invoice_due_date(),
  159. 'header' => $this->header(),
  160. 'subheader' => $this->subheader(),
  161. 'accent_color' => $this->accent_color(),
  162. 'font_family' => $this->fontFamily(),
  163. 'font_html' => $this->font($this->fontFamily())->getFontHtml(),
  164. 'footer' => $this->footer(),
  165. 'terms' => $this->terms(),
  166. 'item_name' => $this->item_name(),
  167. 'unit_name' => $this->unit_name(),
  168. 'price_name' => $this->price_name(),
  169. 'amount_name' => $this->amount_name(),
  170. ];
  171. }
  172. }