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.

DocumentDefault.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Casts\TrimLeadingZeroCast;
  4. use App\Enums\DocumentType;
  5. use App\Enums\Font;
  6. use App\Enums\PaymentTerms;
  7. use App\Enums\Template;
  8. use App\Traits\Blamable;
  9. use App\Traits\CompanyOwned;
  10. use Database\Factories\Setting\DocumentDefaultFactory;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Database\Eloquent\Casts\AsArrayObject;
  13. use Illuminate\Database\Eloquent\Factories\Factory;
  14. use Illuminate\Database\Eloquent\Factories\HasFactory;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  17. use Wallo\FilamentCompanies\FilamentCompanies;
  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. public function company(): BelongsTo
  59. {
  60. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  61. }
  62. public function createdBy(): BelongsTo
  63. {
  64. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  65. }
  66. public function updatedBy(): BelongsTo
  67. {
  68. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  69. }
  70. public function scopeType(Builder $query, string | DocumentType $type): Builder
  71. {
  72. return $query->where($this->qualifyColumn('type'), $type);
  73. }
  74. public function scopeInvoice(Builder $query): Builder
  75. {
  76. return $query->scopes(['type' => [DocumentType::Invoice]]);
  77. }
  78. public function scopeBill(Builder $query): Builder
  79. {
  80. return $query->scopes(['type' => [DocumentType::Bill]]);
  81. }
  82. public static function availableNumberDigits(): array
  83. {
  84. return array_combine(range(1, 20), range(1, 20));
  85. }
  86. public static function getNumberNext(?bool $padded = null, ?bool $format = null, ?string $prefix = null, int | string | null $digits = null, int | string | null $next = null, ?string $type = null): string
  87. {
  88. $initializeAttributes = new static;
  89. [$number_prefix, $number_digits, $number_next] = $initializeAttributes->initializeAttributes($prefix, $digits, $next, $type);
  90. if ($format) {
  91. return $number_prefix . static::getPaddedNumberNext($number_next, $number_digits);
  92. }
  93. if ($padded) {
  94. return static::getPaddedNumberNext($number_next, $number_digits);
  95. }
  96. return $number_next;
  97. }
  98. public function initializeAttributes(?string $prefix, int | string | null $digits, int | string | null $next, ?string $type): array
  99. {
  100. $number_prefix = $prefix ?? $this->getAttributeFromArray('number_prefix');
  101. $number_digits = $digits ?? $this->getAttributeFromArray('number_digits');
  102. $number_next = $next ?? $this->getAttributeFromArray('number_next');
  103. if ($type) {
  104. $attributes = static::getAttributesByType($type);
  105. $number_prefix = $attributes['number_prefix'] ?? $number_prefix;
  106. $number_digits = $attributes['number_digits'] ?? $number_digits;
  107. $number_next = $attributes['number_next'] ?? $number_next;
  108. }
  109. return [$number_prefix, $number_digits, $number_next];
  110. }
  111. public static function getAttributesByType(?string $type): array
  112. {
  113. $model = new static;
  114. $attributes = $model->newQuery()->type($type)->first();
  115. return $attributes ? $attributes->toArray() : [];
  116. }
  117. /**
  118. * Get the next number with padding for dynamic display purposes.
  119. * Even if number_next is a string, it will be cast to an integer.
  120. */
  121. public static function getPaddedNumberNext(int | string | null $number_next, int | string | null $number_digits): string
  122. {
  123. return str_pad($number_next, $number_digits, '0', STR_PAD_LEFT);
  124. }
  125. public static function getAvailableItemNameOptions(): array
  126. {
  127. $options = [
  128. 'items' => 'Items',
  129. 'products' => 'Products',
  130. 'services' => 'Services',
  131. 'other' => 'Other',
  132. ];
  133. return array_map(translate(...), $options);
  134. }
  135. public static function getAvailableUnitNameOptions(): array
  136. {
  137. $options = [
  138. 'quantity' => 'Quantity',
  139. 'hours' => 'Hours',
  140. 'other' => 'Other',
  141. ];
  142. return array_map(translate(...), $options);
  143. }
  144. public static function getAvailablePriceNameOptions(): array
  145. {
  146. $options = [
  147. 'price' => 'Price',
  148. 'rate' => 'Rate',
  149. 'other' => 'Other',
  150. ];
  151. return array_map(translate(...), $options);
  152. }
  153. public static function getAvailableAmountNameOptions(): array
  154. {
  155. $options = [
  156. 'amount' => 'Amount',
  157. 'total' => 'Total',
  158. 'other' => 'Other',
  159. ];
  160. return array_map(translate(...), $options);
  161. }
  162. public function getLabelOptionFor(string $optionType, ?string $optionValue)
  163. {
  164. $optionValue = $optionValue ?? $this->{$optionType}['option'];
  165. if (! $optionValue) {
  166. return null;
  167. }
  168. $options = match ($optionType) {
  169. 'item_name' => static::getAvailableItemNameOptions(),
  170. 'unit_name' => static::getAvailableUnitNameOptions(),
  171. 'price_name' => static::getAvailablePriceNameOptions(),
  172. 'amount_name' => static::getAvailableAmountNameOptions(),
  173. default => [],
  174. };
  175. return $options[$optionValue] ?? null;
  176. }
  177. protected static function newFactory(): Factory
  178. {
  179. return DocumentDefaultFactory::new();
  180. }
  181. }