CommonCatalogItem.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use App\Helpers\Price;
  5. use Database\Factories\CommonCatalogItemFactory;
  6. use Illuminate\Database\Eloquent\Casts\Attribute;
  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\BelongsToMany;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Illuminate\Database\Eloquent\Relations\HasOne;
  13. use Illuminate\Database\Eloquent\SoftDeletes;
  14. class CommonCatalogItem extends Model
  15. {
  16. /** @use HasFactory<CommonCatalogItemFactory> */
  17. use HasFactory;
  18. use SoftDeletes;
  19. public const DEFAULT_SORT_BY = 'article';
  20. public const PRICE_FIELDS = [
  21. 'builders_price' => 'строители',
  22. 'wholesale_price' => 'опт',
  23. 'recommended_price' => 'рек',
  24. 'retail_price' => 'розница',
  25. 'project_price' => 'проект',
  26. 'project_with_installation_price' => 'проект+м',
  27. 'pik_price' => 'пик',
  28. 'recommended_plus_10_price' => 'рек+10',
  29. ];
  30. public const TECHNICAL_DESCRIPTION_FIELDS = [
  31. 'print_name',
  32. 'product_group',
  33. 'characteristics',
  34. 'technical_description',
  35. 'technical_description_short',
  36. ];
  37. protected $fillable = [
  38. 'image_file_id',
  39. 'article',
  40. 'calculator_name',
  41. 'print_name',
  42. 'kind',
  43. 'product_group',
  44. 'dimension_length',
  45. 'dimension_width',
  46. 'dimension_height',
  47. 'site_length',
  48. 'site_width',
  49. 'dimensions',
  50. 'fall_height',
  51. 'additional_info',
  52. 'dimension_unit',
  53. 'weight',
  54. 'volume',
  55. 'places',
  56. 'composition',
  57. 'characteristics',
  58. 'technical_description',
  59. 'technical_description_short',
  60. 'age_group',
  61. 'max_users',
  62. 'unit',
  63. 'series',
  64. 'trademark',
  65. 'calculator_enabled',
  66. 'builders_price',
  67. 'wholesale_price',
  68. 'recommended_price',
  69. 'retail_price',
  70. 'project_price',
  71. 'project_with_installation_price',
  72. 'pik_price',
  73. 'recommended_plus_10_price',
  74. 'note',
  75. ];
  76. protected $appends = [
  77. 'image',
  78. 'calculator_enabled_txt',
  79. 'builders_price_txt',
  80. 'wholesale_price_txt',
  81. 'recommended_price_txt',
  82. 'retail_price_txt',
  83. 'project_price_txt',
  84. 'project_with_installation_price_txt',
  85. 'pik_price_txt',
  86. 'recommended_plus_10_price_txt',
  87. ];
  88. protected function casts(): array
  89. {
  90. return [
  91. 'fall_height' => 'decimal:3',
  92. 'weight' => 'decimal:3',
  93. 'volume' => 'decimal:3',
  94. 'places' => 'integer',
  95. 'max_users' => 'integer',
  96. 'calculator_enabled' => 'boolean',
  97. ];
  98. }
  99. protected function buildersPrice(): Attribute
  100. {
  101. return $this->priceAttribute();
  102. }
  103. protected function wholesalePrice(): Attribute
  104. {
  105. return $this->priceAttribute();
  106. }
  107. protected function recommendedPrice(): Attribute
  108. {
  109. return $this->priceAttribute();
  110. }
  111. protected function retailPrice(): Attribute
  112. {
  113. return $this->priceAttribute();
  114. }
  115. protected function projectPrice(): Attribute
  116. {
  117. return $this->priceAttribute();
  118. }
  119. protected function projectWithInstallationPrice(): Attribute
  120. {
  121. return $this->priceAttribute();
  122. }
  123. protected function pikPrice(): Attribute
  124. {
  125. return $this->priceAttribute();
  126. }
  127. protected function recommendedPlus10Price(): Attribute
  128. {
  129. return $this->priceAttribute();
  130. }
  131. protected function calculatorEnabledTxt(): Attribute
  132. {
  133. return Attribute::make(
  134. get: fn (): string => match ($this->calculator_enabled) {
  135. true => 'да',
  136. false => 'нет',
  137. null => '',
  138. },
  139. );
  140. }
  141. protected function buildersPriceTxt(): Attribute
  142. {
  143. return $this->formattedPriceAttribute('builders_price');
  144. }
  145. protected function wholesalePriceTxt(): Attribute
  146. {
  147. return $this->formattedPriceAttribute('wholesale_price');
  148. }
  149. protected function recommendedPriceTxt(): Attribute
  150. {
  151. return $this->formattedPriceAttribute('recommended_price');
  152. }
  153. protected function retailPriceTxt(): Attribute
  154. {
  155. return $this->formattedPriceAttribute('retail_price');
  156. }
  157. protected function projectPriceTxt(): Attribute
  158. {
  159. return $this->formattedPriceAttribute('project_price');
  160. }
  161. protected function projectWithInstallationPriceTxt(): Attribute
  162. {
  163. return $this->formattedPriceAttribute('project_with_installation_price');
  164. }
  165. protected function pikPriceTxt(): Attribute
  166. {
  167. return $this->formattedPriceAttribute('pik_price');
  168. }
  169. protected function recommendedPlus10PriceTxt(): Attribute
  170. {
  171. return $this->formattedPriceAttribute('recommended_plus_10_price');
  172. }
  173. public function imageFile(): BelongsTo
  174. {
  175. return $this->belongsTo(File::class, 'image_file_id');
  176. }
  177. public function documents(): BelongsToMany
  178. {
  179. return $this->belongsToMany(
  180. File::class,
  181. 'common_catalog_item_documents',
  182. )->withTimestamps();
  183. }
  184. public function stockOrders(): HasMany
  185. {
  186. return $this->hasMany(StockOrder::class);
  187. }
  188. public function latestStockOrder(): HasOne
  189. {
  190. return $this->hasOne(StockOrder::class)->latestOfMany();
  191. }
  192. public function stockReservations(): HasMany
  193. {
  194. return $this->hasMany(StockReservation::class);
  195. }
  196. public function stockMovements(): HasMany
  197. {
  198. return $this->hasMany(StockMovement::class);
  199. }
  200. protected function image(): Attribute
  201. {
  202. return Attribute::make(
  203. get: fn (): string => (string) ($this->imageFile?->thumbnail_link ?? ''),
  204. );
  205. }
  206. private function priceAttribute(): Attribute
  207. {
  208. return Attribute::make(
  209. get: fn (mixed $value): ?float => $value === null ? null : (int) $value / 100,
  210. set: fn (mixed $value): ?int => $value === null || $value === ''
  211. ? null
  212. : (int) round((float) $value * 100),
  213. );
  214. }
  215. private function formattedPriceAttribute(string $field): Attribute
  216. {
  217. return Attribute::make(
  218. get: fn (): string => isset($this->attributes[$field]) && $this->attributes[$field] !== null
  219. ? Price::format((int) $this->attributes[$field] / 100)
  220. : '-',
  221. );
  222. }
  223. }