PricingCode.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  5. class PricingCode extends Model
  6. {
  7. const TYPE_TSN_NUMBER = 'tsn_number';
  8. const TYPE_PRICING_CODE = 'pricing_code';
  9. const DEFAULT_SORT_BY = 'created_at';
  10. protected $fillable = [
  11. 'type',
  12. 'code',
  13. 'description',
  14. ];
  15. public function spareParts(): BelongsToMany
  16. {
  17. return $this->belongsToMany(SparePart::class, 'spare_part_pricing_code')
  18. ->withTimestamps();
  19. }
  20. /**
  21. * Получить расшифровку для № по ТСН
  22. */
  23. public static function getTsnDescription(?string $code): ?string
  24. {
  25. if (!$code) {
  26. return null;
  27. }
  28. return self::where('type', self::TYPE_TSN_NUMBER)
  29. ->where('code', $code)
  30. ->value('description');
  31. }
  32. /**
  33. * Получить расшифровку для шифра расценки
  34. */
  35. public static function getPricingCodeDescription(?string $code): ?string
  36. {
  37. if (!$code) {
  38. return null;
  39. }
  40. return self::where('type', self::TYPE_PRICING_CODE)
  41. ->where('code', $code)
  42. ->value('description');
  43. }
  44. /**
  45. * Создать или обновить запись справочника
  46. */
  47. public static function createOrUpdate(string $type, string $code, string $description): void
  48. {
  49. self::updateOrCreate(
  50. ['type' => $type, 'code' => $code],
  51. ['description' => $description]
  52. );
  53. }
  54. }