Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DocumentDefault.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Document\Document;
  4. use App\Scopes\CurrentCompanyScope;
  5. use App\Traits\Blamable;
  6. use App\Traits\CompanyOwned;
  7. use Database\Factories\DocumentDefaultFactory;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Wallo\FilamentCompanies\FilamentCompanies;
  14. class DocumentDefault extends Model
  15. {
  16. use Blamable, CompanyOwned, HasFactory;
  17. protected $table = 'document_defaults';
  18. protected $fillable = [
  19. 'type',
  20. 'document_logo',
  21. 'number_prefix',
  22. 'number_digits',
  23. 'number_next',
  24. 'payment_terms',
  25. 'title',
  26. 'subheading',
  27. 'terms',
  28. 'footer',
  29. 'accent_color',
  30. 'template',
  31. 'item_column',
  32. 'unit_column',
  33. 'price_column',
  34. 'amount_column',
  35. 'created_by',
  36. 'updated_by',
  37. ];
  38. public function company(): BelongsTo
  39. {
  40. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  41. }
  42. public function documents(): HasMany
  43. {
  44. return $this->hasMany(Document::class);
  45. }
  46. public function createdBy(): BelongsTo
  47. {
  48. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  49. }
  50. public function updatedBy(): BelongsTo
  51. {
  52. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  53. }
  54. public static function getAvailableNumberDigits(): array
  55. {
  56. return array_combine(range(1, 20), range(1, 20));
  57. }
  58. public static function getDefaultNumberDigits(string $type = 'invoice'): int
  59. {
  60. return static::where('type', $type)->value('number_digits') ?? 5;
  61. }
  62. public static function getNextDocumentNumber(int|null $numDigits = null, string $type = 'invoice'): string
  63. {
  64. $latestDocument = Document::where('type', $type)->orderBy('id', 'desc')->first();
  65. $nextNumber = $latestDocument ? ((int)$latestDocument->number + 1) : 1;
  66. return str_pad($nextNumber, $numDigits, '0', STR_PAD_LEFT);
  67. }
  68. public static function getPaymentTerms(): array
  69. {
  70. return [
  71. 0 => 'Due on Receipt',
  72. 7 => 'Net 7',
  73. 10 => 'Net 10',
  74. 15 => 'Net 15',
  75. 30 => 'Net 30',
  76. 60 => 'Net 60',
  77. 90 => 'Net 90',
  78. ];
  79. }
  80. public static function getDefaultPaymentTerms(string $type = 'invoice'): int
  81. {
  82. return static::where('type', $type)->value('payment_terms') ?? 30;
  83. }
  84. public static function getItemColumns(): array
  85. {
  86. return [
  87. 'items' => 'Items',
  88. 'products' => 'Products',
  89. 'services' => 'Services',
  90. 'other' => 'Other',
  91. ];
  92. }
  93. public static function getDefaultItemColumn(string $type = 'invoice'): string
  94. {
  95. return static::where('type', $type)->value('item_column') ?? 'items';
  96. }
  97. public static function getUnitColumns(): array
  98. {
  99. return [
  100. 'quantity' => 'Quantity',
  101. 'hours' => 'Hours',
  102. 'other' => 'Other',
  103. ];
  104. }
  105. public static function getDefaultUnitColumn(string $type = 'invoice'): string
  106. {
  107. return static::where('type', $type)->value('unit_column') ?? 'quantity';
  108. }
  109. public static function getPriceColumns(): array
  110. {
  111. return [
  112. 'price' => 'Price',
  113. 'rate' => 'Rate',
  114. 'other' => 'Other',
  115. ];
  116. }
  117. public static function getDefaultPriceColumn(string $type = 'invoice'): string
  118. {
  119. return static::where('type', $type)->value('price_column') ?? 'price';
  120. }
  121. public static function getAmountColumns(): array
  122. {
  123. return [
  124. 'amount' => 'Amount',
  125. 'total' => 'Total',
  126. 'other' => 'Other',
  127. ];
  128. }
  129. public static function getDefaultAmountColumn(string $type = 'invoice'): string
  130. {
  131. return static::where('type', $type)->value('amount_column') ?? 'amount';
  132. }
  133. public function getDocumentNumberAttribute(): string
  134. {
  135. return $this->document_number_prefix . str_pad($this->document_number_next, $this->document_number_digits, '0', STR_PAD_LEFT);
  136. }
  137. protected static function newFactory(): Factory
  138. {
  139. return DocumentDefaultFactory::new();
  140. }
  141. }