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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\View\Models;
  3. use App\Enums\{Font, PaymentTerms};
  4. use App\Models\Setting\DocumentDefault;
  5. use Filament\Panel\Concerns\HasFont;
  6. class InvoiceViewModel
  7. {
  8. use HasFont;
  9. public DocumentDefault $invoice;
  10. public ?array $data = [];
  11. public function __construct(DocumentDefault $invoice, ?array $data = null)
  12. {
  13. $this->invoice = $invoice;
  14. $this->data = $data;
  15. }
  16. public function logo(): ?string
  17. {
  18. return $this->invoice->logo ?? null;
  19. }
  20. public function show_logo(): bool
  21. {
  22. return $this->data['show_logo'] ?? $this->invoice->show_logo ?? false;
  23. }
  24. // Company related methods
  25. public function company_name(): string
  26. {
  27. return $this->invoice->company->name;
  28. }
  29. public function company_address(): ?string
  30. {
  31. return $this->invoice->company->profile->address ?? null;
  32. }
  33. public function company_phone(): ?string
  34. {
  35. return $this->invoice->company->profile->phone_number ?? null;
  36. }
  37. public function company_city(): ?string
  38. {
  39. return $this->invoice->company->profile->city ?? null;
  40. }
  41. public function company_state(): ?string
  42. {
  43. return $this->invoice->company->profile->state ?? null;
  44. }
  45. public function company_zip(): ?string
  46. {
  47. return $this->invoice->company->profile->zip_code ?? null;
  48. }
  49. public function company_country(): ?string
  50. {
  51. return $this->invoice->company->profile->getCountryName();
  52. }
  53. // Invoice numbering related methods
  54. public function number_prefix(): string
  55. {
  56. return $this->data['number_prefix'] ?? $this->invoice->number_prefix ?? 'INV-';
  57. }
  58. public function number_digits(): int
  59. {
  60. return $this->data['number_digits'] ?? $this->invoice->number_digits ?? 5;
  61. }
  62. public function number_next(): string
  63. {
  64. return $this->data['number_next'] ?? $this->invoice->number_next;
  65. }
  66. public function invoice_number(): string
  67. {
  68. return DocumentDefault::getNumberNext(padded: true, format: true, prefix: $this->number_prefix(), digits: $this->number_digits(), next: $this->number_next());
  69. }
  70. // Invoice date related methods
  71. public function invoice_date(): string
  72. {
  73. return now()->format('M d, Y');
  74. }
  75. public function payment_terms(): string
  76. {
  77. return $this->data['payment_terms'] ?? $this->invoice->payment_terms?->value ?? PaymentTerms::DEFAULT;
  78. }
  79. public function invoice_due_date(): string
  80. {
  81. return PaymentTerms::tryFrom($this->payment_terms())?->getDueDate();
  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 ?? 'Thank you for your business!';
  110. }
  111. public function terms(): string
  112. {
  113. return $this->data['terms'] ?? $this->invoice->terms ?? 'Payment is due within thirty (30) days from the date of invoice. Any discrepancies should be reported within fourteen (14) days of receipt.';
  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 ? ucwords($option) : $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. }