Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentDefault.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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
  84. {
  85. $initializeAttributes = new static;
  86. [$number_prefix, $number_digits, $number_next] = $initializeAttributes->initializeAttributes($prefix, $digits, $next, $type);
  87. if ($format) {
  88. return $number_prefix . static::getPaddedNumberNext($number_next, $number_digits);
  89. }
  90. if ($padded) {
  91. return static::getPaddedNumberNext($number_next, $number_digits);
  92. }
  93. return $number_next;
  94. }
  95. public function initializeAttributes(?string $prefix, int | string | null $digits, int | string | null $next, ?string $type): array
  96. {
  97. $number_prefix = $prefix ?? $this->getAttributeFromArray('number_prefix');
  98. $number_digits = $digits ?? $this->getAttributeFromArray('number_digits');
  99. $number_next = $next ?? $this->getAttributeFromArray('number_next');
  100. if ($type) {
  101. $attributes = static::getAttributesByType($type);
  102. $number_prefix = $attributes['number_prefix'] ?? $number_prefix;
  103. $number_digits = $attributes['number_digits'] ?? $number_digits;
  104. $number_next = $attributes['number_next'] ?? $number_next;
  105. }
  106. return [$number_prefix, $number_digits, $number_next];
  107. }
  108. public static function getAttributesByType(?string $type): array
  109. {
  110. $model = new static;
  111. $attributes = $model->newQuery()->type($type)->first();
  112. return $attributes ? $attributes->toArray() : [];
  113. }
  114. /**
  115. * Get the next number with padding for dynamic display purposes.
  116. * Even if number_next is a string, it will be cast to an integer.
  117. */
  118. public static function getPaddedNumberNext(int | string | null $number_next, int | string | null $number_digits): string
  119. {
  120. return str_pad($number_next, $number_digits, '0', STR_PAD_LEFT);
  121. }
  122. public static function getAvailableItemNameOptions(): array
  123. {
  124. $options = [
  125. 'items' => 'Items',
  126. 'products' => 'Products',
  127. 'services' => 'Services',
  128. 'other' => 'Other',
  129. ];
  130. return array_map(translate(...), $options);
  131. }
  132. public static function getAvailableUnitNameOptions(): array
  133. {
  134. $options = [
  135. 'quantity' => 'Quantity',
  136. 'hours' => 'Hours',
  137. 'other' => 'Other',
  138. ];
  139. return array_map(translate(...), $options);
  140. }
  141. public static function getAvailablePriceNameOptions(): array
  142. {
  143. $options = [
  144. 'price' => 'Price',
  145. 'rate' => 'Rate',
  146. 'other' => 'Other',
  147. ];
  148. return array_map(translate(...), $options);
  149. }
  150. public static function getAvailableAmountNameOptions(): array
  151. {
  152. $options = [
  153. 'amount' => 'Amount',
  154. 'total' => 'Total',
  155. 'other' => 'Other',
  156. ];
  157. return array_map(translate(...), $options);
  158. }
  159. public function getLabelOptionFor(string $optionType, ?string $optionValue)
  160. {
  161. $optionValue = $optionValue ?? $this->{$optionType}['option'];
  162. if (! $optionValue) {
  163. return null;
  164. }
  165. $options = match ($optionType) {
  166. 'item_name' => static::getAvailableItemNameOptions(),
  167. 'unit_name' => static::getAvailableUnitNameOptions(),
  168. 'price_name' => static::getAvailablePriceNameOptions(),
  169. 'amount_name' => static::getAvailableAmountNameOptions(),
  170. default => [],
  171. };
  172. return $options[$optionValue] ?? null;
  173. }
  174. protected static function newFactory(): Factory
  175. {
  176. return DocumentDefaultFactory::new();
  177. }
  178. }