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

InvoiceViewModel.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\View\Models;
  3. use App\Models\Setting\DocumentDefault;
  4. use Spatie\ViewModels\ViewModel;
  5. class InvoiceViewModel extends ViewModel
  6. {
  7. public DocumentDefault $invoice;
  8. public ?array $data = [];
  9. public function __construct(DocumentDefault $invoice, ?array $data = null)
  10. {
  11. $this->invoice = $invoice;
  12. $this->data = $data;
  13. }
  14. public function document_logo(): ?string
  15. {
  16. return $this->data['document_logo'] ?? $this->invoice->document_logo ?? $this->invoice->company->logo ?? null;
  17. }
  18. // Company related methods
  19. public function company_name(): string
  20. {
  21. return $this->invoice->company->name;
  22. }
  23. public function company_address(): ?string
  24. {
  25. return $this->invoice->company->address ?? null;
  26. }
  27. public function company_phone(): ?string
  28. {
  29. return $this->invoice->company->phone ?? null;
  30. }
  31. public function company_city(): ?string
  32. {
  33. return $this->invoice->company->city ?? null;
  34. }
  35. public function company_state(): ?string
  36. {
  37. return $this->invoice->company->state ?? null;
  38. }
  39. public function company_zip(): ?string
  40. {
  41. return $this->invoice->company->zip_code ?? null;
  42. }
  43. // Invoice numbering related methods
  44. public function number_prefix(): string
  45. {
  46. return $this->data['number_prefix'] ?? $this->invoice->number_prefix ?? 'INV-';
  47. }
  48. public function number_digits(): int
  49. {
  50. return $this->data['number_digits'] ?? $this->invoice->number_digits ?? $this->invoice->getDefaultNumberDigits();
  51. }
  52. public function number_next(): int
  53. {
  54. return $this->data['number_next'] ?? $this->invoice->number_next ?? $this->invoice->getNextDocumentNumber($this->number_digits());
  55. }
  56. public function invoice_number(): string
  57. {
  58. return $this->number_prefix() . str_pad($this->number_next(), $this->number_digits(), "0", STR_PAD_LEFT);
  59. }
  60. // Invoice date related methods
  61. public function invoice_date(): string
  62. {
  63. return now()->format('M d, Y');
  64. }
  65. public function payment_terms(): string
  66. {
  67. return $this->data['payment_terms'] ?? $this->invoice->payment_terms ?? $this->invoice->getDefaultPaymentTerms();
  68. }
  69. public function invoice_due_date(): string
  70. {
  71. return now()->addDays($this->payment_terms())->format('M d, Y');
  72. }
  73. // Invoice header related methods
  74. public function title(): string
  75. {
  76. return $this->data['title'] ?? $this->invoice->title ?? 'Invoice';
  77. }
  78. public function subheading(): ?string
  79. {
  80. return $this->data['subheading'] ?? $this->invoice->subheading ?? null;
  81. }
  82. // Invoice styling
  83. public function accent_color(): string
  84. {
  85. return $this->data['accent_color'] ?? $this->invoice->accent_color ?? '#6366F1';
  86. }
  87. public function footer(): string
  88. {
  89. return $this->data['footer'] ?? $this->invoice->footer ?? 'Thank you for your business!';
  90. }
  91. public function terms(): string
  92. {
  93. 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.';
  94. }
  95. // Invoice column related methods
  96. public function item_column(): string
  97. {
  98. $item_column = $this->data['item_column'] ?? $this->invoice->item_column ?? $this->invoice->getDefaultItemColumn();
  99. return isset($this->invoice->getItemColumns()[$item_column]) ? ucfirst($item_column) : $item_column;
  100. }
  101. public function unit_column(): string
  102. {
  103. $unit_column = $this->data['unit_column'] ?? $this->invoice->unit_column ?? $this->invoice->getDefaultUnitColumn();
  104. return isset($this->invoice->getUnitColumns()[$unit_column]) ? ucfirst($unit_column) : $unit_column;
  105. }
  106. public function price_column(): string
  107. {
  108. $price_column = $this->data['price_column'] ?? $this->invoice->price_column ?? $this->invoice->getDefaultPriceColumn();
  109. return isset($this->invoice->getPriceColumns()[$price_column]) ? ucfirst($price_column) : $price_column;
  110. }
  111. public function amount_column(): string
  112. {
  113. $amount_column = $this->data['amount_column'] ?? $this->invoice->amount_column ?? $this->invoice->getDefaultAmountColumn();
  114. return isset($this->invoice->getAmountColumns()[$amount_column]) ? ucfirst($amount_column) : $amount_column;
  115. }
  116. public function buildViewData(): array
  117. {
  118. return [
  119. 'document_logo' => $this->document_logo(),
  120. 'company_name' => $this->company_name(),
  121. 'company_address' => $this->company_address(),
  122. 'company_phone' => $this->company_phone(),
  123. 'company_city' => $this->company_city(),
  124. 'company_state' => $this->company_state(),
  125. 'company_zip' => $this->company_zip(),
  126. 'number_prefix' => $this->number_prefix(),
  127. 'number_digits' => $this->number_digits(),
  128. 'number_next' => $this->number_next(),
  129. 'invoice_number' => $this->invoice_number(),
  130. 'invoice_date' => $this->invoice_date(),
  131. 'payment_terms' => $this->payment_terms(),
  132. 'invoice_due_date' => $this->invoice_due_date(),
  133. 'title' => $this->title(),
  134. 'subheading' => $this->subheading(),
  135. 'accent_color' => $this->accent_color(),
  136. 'footer' => $this->footer(),
  137. 'terms' => $this->terms(),
  138. 'item_column' => $this->item_column(),
  139. 'unit_column' => $this->unit_column(),
  140. 'price_column' => $this->price_column(),
  141. 'amount_column' => $this->amount_column(),
  142. ];
  143. }
  144. }