You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentDefault.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Document\Document;
  4. use App\Scopes\CurrentCompanyScope;
  5. use Database\Factories\DocumentDefaultFactory;
  6. use Illuminate\Database\Eloquent\Factories\Factory;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Wallo\FilamentCompanies\FilamentCompanies;
  12. class DocumentDefault extends Model
  13. {
  14. use HasFactory;
  15. protected $table = 'document_defaults';
  16. protected $fillable = [
  17. 'company_id',
  18. 'type',
  19. 'document_number_prefix',
  20. 'document_number_digits',
  21. 'document_number_next',
  22. 'payment_terms',
  23. 'template',
  24. 'title',
  25. 'subheading',
  26. 'notes',
  27. 'terms',
  28. 'footer',
  29. ];
  30. protected static function booted(): void
  31. {
  32. static::addGlobalScope(new CurrentCompanyScope);
  33. }
  34. public function company(): BelongsTo
  35. {
  36. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  37. }
  38. public function documents(): HasMany
  39. {
  40. return $this->hasMany(Document::class);
  41. }
  42. public static function getDocumentNumberDigits(): array
  43. {
  44. return array_combine(range(1, 20), range(1, 20));
  45. }
  46. public static function getDefaultDocumentNumberDigits(string $type = 'invoice'): int
  47. {
  48. $documentNumberDigits = self::where('type', $type)->pluck('document_number_digits', 'id')->toArray();
  49. return array_key_first($documentNumberDigits) ?? 5;
  50. }
  51. public static function getDefaultDocumentNumberNext(int|null $numDigits = null, string $type = 'invoice'): string
  52. {
  53. // Fetch the latest document
  54. $latestDocument = Document::where('type', $type)->orderBy('id', 'desc')->first();
  55. // If there are no documents yet, start from 1
  56. if (!$latestDocument) {
  57. $nextNumber = 1;
  58. } else {
  59. // Otherwise, increment the latest document's number
  60. $nextNumber = (int)$latestDocument->document_number + 1;
  61. }
  62. return str_pad($nextNumber, $numDigits, '0', STR_PAD_LEFT);
  63. }
  64. public static function getPaymentTerms(): array
  65. {
  66. return [
  67. 0 => 'Due on Receipt',
  68. 7 => 'Net 7',
  69. 10 => 'Net 10',
  70. 15 => 'Net 15',
  71. 30 => 'Net 30',
  72. 60 => 'Net 60',
  73. 90 => 'Net 90',
  74. ];
  75. }
  76. public static function getDefaultPaymentTerms(string $type = 'invoice'): int
  77. {
  78. $paymentTerms = self::where('type', $type)->pluck('payment_terms', 'id')->toArray();
  79. return array_key_first($paymentTerms) ?? 30;
  80. }
  81. public function getDocumentNumberAttribute(): string
  82. {
  83. return $this->document_number_prefix . str_pad($this->document_number_next, $this->document_number_digits, '0', STR_PAD_LEFT);
  84. }
  85. protected static function newFactory(): Factory
  86. {
  87. return DocumentDefaultFactory::new();
  88. }
  89. }