Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

InvoiceViewModel.php 6.0KB

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