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.

DocumentDefault.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Casts\TrimLeadingZeroCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Enums\Setting\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\Builder;
  12. use Illuminate\Database\Eloquent\Casts\AsArrayObject;
  13. use Illuminate\Database\Eloquent\Casts\Attribute;
  14. use Illuminate\Database\Eloquent\Factories\Factory;
  15. use Illuminate\Database\Eloquent\Factories\HasFactory;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Support\Facades\Storage;
  18. class DocumentDefault extends Model
  19. {
  20. use Blamable;
  21. use CompanyOwned;
  22. use HasFactory;
  23. protected $table = 'document_defaults';
  24. protected $fillable = [
  25. 'company_id',
  26. 'type',
  27. 'logo',
  28. 'show_logo',
  29. 'number_prefix',
  30. 'number_digits',
  31. 'number_next',
  32. 'payment_terms',
  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. 'show_logo' => 'boolean',
  49. 'number_next' => TrimLeadingZeroCast::class,
  50. 'payment_terms' => PaymentTerms::class,
  51. 'font' => Font::class,
  52. 'template' => Template::class,
  53. 'item_name' => AsArrayObject::class,
  54. 'unit_name' => AsArrayObject::class,
  55. 'price_name' => AsArrayObject::class,
  56. 'amount_name' => AsArrayObject::class,
  57. ];
  58. protected $appends = [
  59. 'logo_url',
  60. ];
  61. protected function logoUrl(): Attribute
  62. {
  63. return Attribute::get(static function (mixed $value, array $attributes): ?string {
  64. return $attributes['logo'] ? Storage::disk('public')->url($attributes['logo']) : null;
  65. });
  66. }
  67. public function scopeType(Builder $query, string | DocumentType $type): Builder
  68. {
  69. return $query->where($this->qualifyColumn('type'), $type);
  70. }
  71. public function scopeInvoice(Builder $query): Builder
  72. {
  73. return $query->scopes(['type' => [DocumentType::Invoice]]);
  74. }
  75. public function scopeBill(Builder $query): Builder
  76. {
  77. return $query->scopes(['type' => [DocumentType::Bill]]);
  78. }
  79. public static function availableNumberDigits(): array
  80. {
  81. return array_combine(range(1, 20), range(1, 20));
  82. }
  83. public function getNumberNext(?bool $padded = null, ?bool $format = null, ?string $prefix = null, int | string | null $digits = null, int | string | null $next = null): string
  84. {
  85. [$number_prefix, $number_digits, $number_next] = $this->initializeAttributes($prefix, $digits, $next);
  86. return match (true) {
  87. $format && $padded => $number_prefix . $this->getPaddedNumberNext($number_next, $number_digits),
  88. $format => $number_prefix . $number_next,
  89. $padded => $this->getPaddedNumberNext($number_next, $number_digits),
  90. default => $number_next,
  91. };
  92. }
  93. public function initializeAttributes(?string $prefix, int | string | null $digits, int | string | null $next): array
  94. {
  95. $number_prefix = $prefix ?? $this->number_prefix;
  96. $number_digits = $digits ?? $this->number_digits;
  97. $number_next = $next ?? $this->number_next;
  98. return [$number_prefix, $number_digits, $number_next];
  99. }
  100. /**
  101. * Get the next number with padding for dynamic display purposes.
  102. * Even if number_next is a string, it will be cast to an integer.
  103. */
  104. public function getPaddedNumberNext(int | string | null $number_next, int | string | null $number_digits): string
  105. {
  106. return str_pad($number_next, $number_digits, '0', STR_PAD_LEFT);
  107. }
  108. public static function getAvailableItemNameOptions(): array
  109. {
  110. $options = [
  111. 'items' => 'Items',
  112. 'products' => 'Products',
  113. 'services' => 'Services',
  114. 'other' => 'Other',
  115. ];
  116. return array_map(translate(...), $options);
  117. }
  118. public static function getAvailableUnitNameOptions(): array
  119. {
  120. $options = [
  121. 'quantity' => 'Quantity',
  122. 'hours' => 'Hours',
  123. 'other' => 'Other',
  124. ];
  125. return array_map(translate(...), $options);
  126. }
  127. public static function getAvailablePriceNameOptions(): array
  128. {
  129. $options = [
  130. 'price' => 'Price',
  131. 'rate' => 'Rate',
  132. 'other' => 'Other',
  133. ];
  134. return array_map(translate(...), $options);
  135. }
  136. public static function getAvailableAmountNameOptions(): array
  137. {
  138. $options = [
  139. 'amount' => 'Amount',
  140. 'total' => 'Total',
  141. 'other' => 'Other',
  142. ];
  143. return array_map(translate(...), $options);
  144. }
  145. public function getLabelOptionFor(string $optionType, ?string $optionValue)
  146. {
  147. $optionValue = $optionValue ?? $this->{$optionType}['option'];
  148. if (! $optionValue) {
  149. return null;
  150. }
  151. $options = match ($optionType) {
  152. 'item_name' => static::getAvailableItemNameOptions(),
  153. 'unit_name' => static::getAvailableUnitNameOptions(),
  154. 'price_name' => static::getAvailablePriceNameOptions(),
  155. 'amount_name' => static::getAvailableAmountNameOptions(),
  156. default => [],
  157. };
  158. return $options[$optionValue] ?? null;
  159. }
  160. protected static function newFactory(): Factory
  161. {
  162. return DocumentDefaultFactory::new();
  163. }
  164. }