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

DocumentDefault.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Casts\TrimLeadingZeroCast;
  4. use App\Enums\DocumentAmountColumn;
  5. use App\Enums\DocumentItemColumn;
  6. use App\Enums\DocumentPriceColumn;
  7. use App\Enums\DocumentType;
  8. use App\Enums\DocumentUnitColumn;
  9. use App\Enums\Font;
  10. use App\Enums\PaymentTerms;
  11. use App\Enums\Template;
  12. use App\Traits\Blamable;
  13. use App\Traits\CompanyOwned;
  14. use Database\Factories\Setting\DocumentDefaultFactory;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Database\Eloquent\Casts\AsArrayObject;
  17. use Illuminate\Database\Eloquent\Factories\Factory;
  18. use Illuminate\Database\Eloquent\Factories\HasFactory;
  19. use Illuminate\Database\Eloquent\Model;
  20. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  21. use Wallo\FilamentCompanies\FilamentCompanies;
  22. class DocumentDefault extends Model
  23. {
  24. use Blamable, CompanyOwned, HasFactory;
  25. protected $table = 'document_defaults';
  26. protected $fillable = [
  27. 'company_id',
  28. 'type',
  29. 'logo',
  30. 'show_logo',
  31. 'number_prefix',
  32. 'number_digits',
  33. 'number_next',
  34. 'payment_terms',
  35. 'header',
  36. 'subheader',
  37. 'terms',
  38. 'footer',
  39. 'accent_color',
  40. 'font',
  41. 'template',
  42. 'item_name',
  43. 'unit_name',
  44. 'price_name',
  45. 'amount_name',
  46. 'created_by',
  47. 'updated_by',
  48. ];
  49. protected $casts = [
  50. 'show_logo' => 'boolean',
  51. 'number_next' => TrimLeadingZeroCast::class,
  52. 'payment_terms' => PaymentTerms::class,
  53. 'font' => Font::class,
  54. 'template' => Template::class,
  55. 'item_name' => AsArrayObject::class,
  56. 'unit_name' => AsArrayObject::class,
  57. 'price_name' => AsArrayObject::class,
  58. 'amount_name' => AsArrayObject::class,
  59. ];
  60. public function company(): BelongsTo
  61. {
  62. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  63. }
  64. public function createdBy(): BelongsTo
  65. {
  66. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  67. }
  68. public function updatedBy(): BelongsTo
  69. {
  70. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  71. }
  72. public function scopeType(Builder $query, string|DocumentType $type): Builder
  73. {
  74. return $query->where($this->qualifyColumn('type'), $type);
  75. }
  76. public function scopeInvoice(Builder $query): Builder
  77. {
  78. return $query->scopes(['type' => [DocumentType::Invoice]]);
  79. }
  80. public function scopeBill(Builder $query): Builder
  81. {
  82. return $query->scopes(['type' => [DocumentType::Bill]]);
  83. }
  84. public static function availableNumberDigits(): array
  85. {
  86. return array_combine(range(1, 20), range(1, 20));
  87. }
  88. 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
  89. {
  90. $initializeAttributes = new static;
  91. [$number_prefix, $number_digits, $number_next] = $initializeAttributes->initializeAttributes($prefix, $digits, $next, $type);
  92. if ($format) {
  93. return $number_prefix . static::getPaddedNumberNext($number_next, $number_digits);
  94. }
  95. if ($padded) {
  96. return static::getPaddedNumberNext($number_next, $number_digits);
  97. }
  98. return $number_next;
  99. }
  100. public function initializeAttributes(?string $prefix, int|string|null $digits, int|string|null $next, ?string $type): array
  101. {
  102. $number_prefix = $prefix ?? $this->getAttributeFromArray('number_prefix');
  103. $number_digits = $digits ?? $this->getAttributeFromArray('number_digits');
  104. $number_next = $next ?? $this->getAttributeFromArray('number_next');
  105. if ($type) {
  106. $attributes = static::getAttributesByType($type);
  107. $number_prefix = $attributes['number_prefix'] ?? $number_prefix;
  108. $number_digits = $attributes['number_digits'] ?? $number_digits;
  109. $number_next = $attributes['number_next'] ?? $number_next;
  110. }
  111. return [$number_prefix, $number_digits, $number_next];
  112. }
  113. public static function getAttributesByType(?string $type): array
  114. {
  115. $model = new static;
  116. $attributes = $model->newQuery()->type($type)->first();
  117. return $attributes ? $attributes->toArray() : [];
  118. }
  119. /**
  120. * Get the next number with padding for dynamic display purposes.
  121. * Even if number_next is a string, it will be cast to an integer.
  122. */
  123. public static function getPaddedNumberNext(int|string|null $number_next, int|string|null $number_digits): string
  124. {
  125. return str_pad($number_next, $number_digits, '0', STR_PAD_LEFT);
  126. }
  127. public function getItemNameOptionAttribute(): ?string
  128. {
  129. return $this->item_name['option'] ?? null;
  130. }
  131. public function getItemNameCustomAttribute(): ?string
  132. {
  133. return $this->item_name['custom'] ?? null;
  134. }
  135. public function getItemNameOptions(): array
  136. {
  137. return $this->item_name ?? [];
  138. }
  139. public function getUnitNameOptionAttribute(): ?string
  140. {
  141. return $this->unit_name['option'] ?? null;
  142. }
  143. public function getUnitNameCustomAttribute(): ?string
  144. {
  145. return $this->unit_name['custom'] ?? null;
  146. }
  147. public function getUnitNameOptions(): array
  148. {
  149. return $this->unit_name ?? [];
  150. }
  151. public function getPriceNameOptionAttribute(): ?string
  152. {
  153. return $this->price_name['option'] ?? null;
  154. }
  155. public function getPriceNameCustomAttribute(): ?string
  156. {
  157. return $this->price_name['custom'] ?? null;
  158. }
  159. public function getPriceNameOptions(): array
  160. {
  161. return $this->price_name ?? [];
  162. }
  163. public function getAmountNameOptionAttribute(): ?string
  164. {
  165. return $this->amount_name['option'] ?? null;
  166. }
  167. public function getAmountNameCustomAttribute(): ?string
  168. {
  169. return $this->amount_name['custom'] ?? null;
  170. }
  171. public function getAmountNameOptions(): array
  172. {
  173. return $this->amount_name ?? [];
  174. }
  175. public static function getAvailableItemNameOptions(): array
  176. {
  177. return [
  178. 'items' => 'Items',
  179. 'products' => 'Products',
  180. 'services' => 'Services',
  181. 'other' => 'Other',
  182. ];
  183. }
  184. public static function getAvailableUnitNameOptions(): array
  185. {
  186. return [
  187. 'quantity' => 'Quantity',
  188. 'hours' => 'Hours',
  189. 'other' => 'Other',
  190. ];
  191. }
  192. public static function getAvailablePriceNameOptions(): array
  193. {
  194. return [
  195. 'price' => 'Price',
  196. 'rate' => 'Rate',
  197. 'other' => 'Other',
  198. ];
  199. }
  200. public static function getAvailableAmountNameOptions(): array
  201. {
  202. return [
  203. 'amount' => 'Amount',
  204. 'total' => 'Total',
  205. 'other' => 'Other',
  206. ];
  207. }
  208. protected static function newFactory(): Factory
  209. {
  210. return DocumentDefaultFactory::new();
  211. }
  212. }