You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentDefault.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Accounting\DocumentDiscountMethod;
  6. use App\Enums\Accounting\DocumentType;
  7. use App\Enums\Setting\Font;
  8. use App\Enums\Setting\PaymentTerms;
  9. use App\Enums\Setting\Template;
  10. use Database\Factories\Setting\DocumentDefaultFactory;
  11. use Illuminate\Database\Eloquent\Attributes\Scope;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Database\Eloquent\Casts\AsArrayObject;
  14. use Illuminate\Database\Eloquent\Casts\Attribute;
  15. use Illuminate\Database\Eloquent\Factories\Factory;
  16. use Illuminate\Database\Eloquent\Factories\HasFactory;
  17. use Illuminate\Database\Eloquent\Model;
  18. use Illuminate\Support\Facades\Storage;
  19. class DocumentDefault extends Model
  20. {
  21. use Blamable;
  22. use CompanyOwned;
  23. use HasFactory;
  24. protected $table = 'document_defaults';
  25. protected $fillable = [
  26. 'company_id',
  27. 'type',
  28. 'logo',
  29. 'show_logo',
  30. 'number_prefix',
  31. 'payment_terms',
  32. 'discount_method',
  33. 'header',
  34. 'subheader',
  35. 'terms',
  36. 'footer',
  37. 'accent_color',
  38. 'font',
  39. 'template',
  40. 'item_name',
  41. 'unit_name',
  42. 'price_name',
  43. 'amount_name',
  44. 'created_by',
  45. 'updated_by',
  46. ];
  47. protected $casts = [
  48. 'type' => DocumentType::class,
  49. 'show_logo' => 'boolean',
  50. 'payment_terms' => PaymentTerms::class,
  51. 'discount_method' => DocumentDiscountMethod::class,
  52. 'font' => Font::class,
  53. 'template' => Template::class,
  54. 'item_name' => AsArrayObject::class,
  55. 'unit_name' => AsArrayObject::class,
  56. 'price_name' => AsArrayObject::class,
  57. 'amount_name' => AsArrayObject::class,
  58. ];
  59. protected $appends = [
  60. 'logo_url',
  61. ];
  62. protected function logoUrl(): Attribute
  63. {
  64. return Attribute::get(static function (mixed $value, array $attributes): ?string {
  65. return $attributes['logo'] ? Storage::disk('public')->url($attributes['logo']) : null;
  66. });
  67. }
  68. #[Scope]
  69. protected function type(Builder $query, string | DocumentType $type): Builder
  70. {
  71. return $query->where('type', $type);
  72. }
  73. #[Scope]
  74. protected function invoice(Builder $query): Builder
  75. {
  76. return $query->type(DocumentType::Invoice);
  77. }
  78. #[Scope]
  79. protected function recurringInvoice(Builder $query): Builder
  80. {
  81. return $query->type(DocumentType::RecurringInvoice);
  82. }
  83. #[Scope]
  84. protected function bill(Builder $query): Builder
  85. {
  86. return $query->type(DocumentType::Bill);
  87. }
  88. #[Scope]
  89. protected function estimate(Builder $query): Builder
  90. {
  91. return $query->type(DocumentType::Estimate);
  92. }
  93. public function getNumberNext(?string $prefix = null, int | string | null $next = null): string
  94. {
  95. $numberPrefix = $prefix ?? $this->number_prefix ?? '';
  96. $numberNext = (string) ($next ?? (static::getBaseNumber() + 1));
  97. return $numberPrefix . $numberNext;
  98. }
  99. public static function getBaseNumber(): int
  100. {
  101. return 1000;
  102. }
  103. public static function getAvailableItemNameOptions(): array
  104. {
  105. $options = [
  106. 'items' => 'Items',
  107. 'products' => 'Products',
  108. 'services' => 'Services',
  109. 'other' => 'Other',
  110. ];
  111. return array_map(translate(...), $options);
  112. }
  113. public static function getAvailableUnitNameOptions(): array
  114. {
  115. $options = [
  116. 'quantity' => 'Quantity',
  117. 'hours' => 'Hours',
  118. 'other' => 'Other',
  119. ];
  120. return array_map(translate(...), $options);
  121. }
  122. public static function getAvailablePriceNameOptions(): array
  123. {
  124. $options = [
  125. 'price' => 'Price',
  126. 'rate' => 'Rate',
  127. 'other' => 'Other',
  128. ];
  129. return array_map(translate(...), $options);
  130. }
  131. public static function getAvailableAmountNameOptions(): array
  132. {
  133. $options = [
  134. 'amount' => 'Amount',
  135. 'total' => 'Total',
  136. 'other' => 'Other',
  137. ];
  138. return array_map(translate(...), $options);
  139. }
  140. public function getLabelOptionFor(string $optionType, ?string $optionValue)
  141. {
  142. $optionValue = $optionValue ?? $this->{$optionType}['option'];
  143. if (! $optionValue) {
  144. return null;
  145. }
  146. $options = match ($optionType) {
  147. 'item_name' => static::getAvailableItemNameOptions(),
  148. 'unit_name' => static::getAvailableUnitNameOptions(),
  149. 'price_name' => static::getAvailablePriceNameOptions(),
  150. 'amount_name' => static::getAvailableAmountNameOptions(),
  151. default => [],
  152. };
  153. return $options[$optionValue] ?? null;
  154. }
  155. public function resolveColumnLabel(string $column, string $default, ?array $data = null): string
  156. {
  157. if ($data) {
  158. $custom = $data[$column]['custom'] ?? null;
  159. $option = $data[$column]['option'] ?? null;
  160. } else {
  161. $custom = $this->{$column}['custom'] ?? null;
  162. $option = $this->{$column}['option'] ?? null;
  163. }
  164. if ($custom) {
  165. return $custom;
  166. }
  167. return $this->getLabelOptionFor($column, $option) ?? $default;
  168. }
  169. protected static function newFactory(): Factory
  170. {
  171. return DocumentDefaultFactory::new();
  172. }
  173. }