PricingCode.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. protected $fillable = [
  10. 'type',
  11. 'code',
  12. 'description',
  13. ];
  14. public function spareParts(): BelongsToMany
  15. {
  16. return $this->belongsToMany(SparePart::class, 'spare_part_pricing_code')
  17. ->withTimestamps();
  18. }
  19. /**
  20. * Получить расшифровку для № по ТСН
  21. */
  22. public static function getTsnDescription(?string $code): ?string
  23. {
  24. if (!$code) {
  25. return null;
  26. }
  27. return self::where('type', self::TYPE_TSN_NUMBER)
  28. ->where('code', $code)
  29. ->value('description');
  30. }
  31. /**
  32. * Получить расшифровку для шифра расценки
  33. */
  34. public static function getPricingCodeDescription(?string $code): ?string
  35. {
  36. if (!$code) {
  37. return null;
  38. }
  39. return self::where('type', self::TYPE_PRICING_CODE)
  40. ->where('code', $code)
  41. ->value('description');
  42. }
  43. /**
  44. * Создать или обновить запись справочника
  45. */
  46. public static function createOrUpdate(string $type, string $code, string $description): void
  47. {
  48. self::updateOrCreate(
  49. ['type' => $type, 'code' => $code],
  50. ['description' => $description]
  51. );
  52. }
  53. }