您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InvoiceViewModel.php 6.9KB

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