Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DocumentDefault.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\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. 'payment_terms',
  31. 'discount_method',
  32. 'header',
  33. 'subheader',
  34. 'terms',
  35. 'footer',
  36. 'accent_color',
  37. 'font',
  38. 'template',
  39. 'item_name',
  40. 'unit_name',
  41. 'price_name',
  42. 'amount_name',
  43. 'created_by',
  44. 'updated_by',
  45. ];
  46. protected $casts = [
  47. 'type' => DocumentType::class,
  48. 'show_logo' => 'boolean',
  49. 'payment_terms' => PaymentTerms::class,
  50. 'discount_method' => DocumentDiscountMethod::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('type', $type);
  70. }
  71. public function scopeInvoice(Builder $query): Builder
  72. {
  73. return $query->type(DocumentType::Invoice);
  74. }
  75. public function scopeRecurringInvoice(Builder $query): Builder
  76. {
  77. return $query->type(DocumentType::RecurringInvoice);
  78. }
  79. public function scopeBill(Builder $query): Builder
  80. {
  81. return $query->type(DocumentType::Bill);
  82. }
  83. public function scopeEstimate(Builder $query): Builder
  84. {
  85. return $query->type(DocumentType::Estimate);
  86. }
  87. public function getNumberNext(?string $prefix = null, int | string | null $next = null): string
  88. {
  89. $numberPrefix = $prefix ?? $this->number_prefix ?? '';
  90. $numberNext = (string) ($next ?? (static::getBaseNumber() + 1));
  91. return $numberPrefix . $numberNext;
  92. }
  93. public static function getBaseNumber(): int
  94. {
  95. return 1000;
  96. }
  97. public static function getAvailableItemNameOptions(): array
  98. {
  99. $options = [
  100. 'items' => 'Items',
  101. 'products' => 'Products',
  102. 'services' => 'Services',
  103. 'other' => 'Other',
  104. ];
  105. return array_map(translate(...), $options);
  106. }
  107. public static function getAvailableUnitNameOptions(): array
  108. {
  109. $options = [
  110. 'quantity' => 'Quantity',
  111. 'hours' => 'Hours',
  112. 'other' => 'Other',
  113. ];
  114. return array_map(translate(...), $options);
  115. }
  116. public static function getAvailablePriceNameOptions(): array
  117. {
  118. $options = [
  119. 'price' => 'Price',
  120. 'rate' => 'Rate',
  121. 'other' => 'Other',
  122. ];
  123. return array_map(translate(...), $options);
  124. }
  125. public static function getAvailableAmountNameOptions(): array
  126. {
  127. $options = [
  128. 'amount' => 'Amount',
  129. 'total' => 'Total',
  130. 'other' => 'Other',
  131. ];
  132. return array_map(translate(...), $options);
  133. }
  134. public function getLabelOptionFor(string $optionType, ?string $optionValue)
  135. {
  136. $optionValue = $optionValue ?? $this->{$optionType}['option'];
  137. if (! $optionValue) {
  138. return null;
  139. }
  140. $options = match ($optionType) {
  141. 'item_name' => static::getAvailableItemNameOptions(),
  142. 'unit_name' => static::getAvailableUnitNameOptions(),
  143. 'price_name' => static::getAvailablePriceNameOptions(),
  144. 'amount_name' => static::getAvailableAmountNameOptions(),
  145. default => [],
  146. };
  147. return $options[$optionValue] ?? null;
  148. }
  149. public function resolveColumnLabel(string $column, string $default, ?array $data = null): string
  150. {
  151. if ($data) {
  152. $custom = $data[$column]['custom'] ?? null;
  153. $option = $data[$column]['option'] ?? null;
  154. } else {
  155. $custom = $this->{$column}['custom'] ?? null;
  156. $option = $this->{$column}['option'] ?? null;
  157. }
  158. if ($custom) {
  159. return $custom;
  160. }
  161. return $this->getLabelOptionFor($column, $option) ?? $default;
  162. }
  163. protected static function newFactory(): Factory
  164. {
  165. return DocumentDefaultFactory::new();
  166. }
  167. }