Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

InvoiceViewModel.php 6.0KB

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