Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InvoiceViewModel.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 ?? PaymentTerms::DEFAULT;
  78. }
  79. public function invoice_due_date(): string
  80. {
  81. $enumPaymentTerms = PaymentTerms::tryFrom($this->payment_terms());
  82. $days = $enumPaymentTerms ? $enumPaymentTerms->getDays() : 0;
  83. return now()->addDays($days)->format('M d, Y');
  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 ?? 'Thank you for your business!';
  112. }
  113. public function terms(): string
  114. {
  115. 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.';
  116. }
  117. // Invoice column related methods
  118. public function item_name(): string
  119. {
  120. $custom_item_name = $this->data['item_name']['custom'] ?? null;
  121. if ($custom_item_name) {
  122. return $custom_item_name;
  123. }
  124. return ucwords($this->data['item_name']['option']) ?? ucwords($this->invoice->item_name_option) ?? $this->invoice->item_name_custom ?? 'Items';
  125. }
  126. public function unit_name(): string
  127. {
  128. $custom_unit_name = $this->data['unit_name']['custom'] ?? null;
  129. if ($custom_unit_name) {
  130. return $custom_unit_name;
  131. }
  132. return ucwords($this->data['unit_name']['option']) ?? ucwords($this->invoice->unit_name_option) ?? $this->invoice->unit_name_custom ?? 'Quantity';
  133. }
  134. public function price_name(): string
  135. {
  136. $custom_price_name = $this->data['price_name']['custom'] ?? null;
  137. if ($custom_price_name) {
  138. return $custom_price_name;
  139. }
  140. return ucwords($this->data['price_name']['option']) ?? ucwords($this->invoice->price_name_option) ?? $this->invoice->price_name_custom ?? 'Price';
  141. }
  142. public function amount_name(): string
  143. {
  144. $custom_amount_name = $this->data['amount_name']['custom'] ?? $this->invoice->amount_name_custom ?? null;
  145. if ($custom_amount_name) {
  146. return $custom_amount_name;
  147. }
  148. return ucwords($this->data['amount_name']['option']) ?? ucwords($this->invoice->amount_name_option) ?? 'Amount';
  149. }
  150. public function buildViewData(): array
  151. {
  152. return [
  153. 'logo' => $this->logo(),
  154. 'show_logo' => $this->show_logo(),
  155. 'company_name' => $this->company_name(),
  156. 'company_address' => $this->company_address(),
  157. 'company_phone' => $this->company_phone(),
  158. 'company_city' => $this->company_city(),
  159. 'company_state' => $this->company_state(),
  160. 'company_zip' => $this->company_zip(),
  161. 'company_country' => $this->company_country(),
  162. 'number_prefix' => $this->number_prefix(),
  163. 'number_digits' => $this->number_digits(),
  164. 'number_next' => $this->number_next(),
  165. 'invoice_number' => $this->invoice_number(),
  166. 'invoice_date' => $this->invoice_date(),
  167. 'payment_terms' => $this->payment_terms(),
  168. 'invoice_due_date' => $this->invoice_due_date(),
  169. 'header' => $this->header(),
  170. 'subheader' => $this->subheader(),
  171. 'accent_color' => $this->accent_color(),
  172. 'font_family' => $this->fontFamily(),
  173. 'font_html' => $this->font($this->fontFamily())->getFontHtml(),
  174. 'footer' => $this->footer(),
  175. 'terms' => $this->terms(),
  176. 'item_name' => $this->item_name(),
  177. 'unit_name' => $this->unit_name(),
  178. 'price_name' => $this->price_name(),
  179. 'amount_name' => $this->amount_name(),
  180. ];
  181. }
  182. }