|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright © 2019 Dxvn, Inc. |
| 7 | + * |
| 8 | + * @author Tran Ngoc Duc <ductn@diepxuan.com> |
| 9 | + * @author Tran Ngoc Duc <caothu91@gmail.com> |
| 10 | + * |
| 11 | + * @lastupdate 2025-08-02 20:04:36 |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Diepxuan\Simba\Models; |
| 15 | + |
| 16 | +use Diepxuan\Simba\SModel\InDmNhvt as Model; |
| 17 | +use Illuminate\Database\Eloquent\Casts\Attribute; |
| 18 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 19 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 20 | +use Illuminate\Support\Collection; |
| 21 | +use Illuminate\Support\Facades\DB; |
| 22 | +use Illuminate\Support\Str; |
| 23 | + |
| 24 | +class InDmNhvt extends Model |
| 25 | +{ |
| 26 | + public const ROOT = 'PRODUCT'; |
| 27 | + |
| 28 | + /** |
| 29 | + * The primary key associated with the table. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + // protected $primaryKey = 'id'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the children Categories. |
| 37 | + */ |
| 38 | + public function catChildrens(): HasMany |
| 39 | + { |
| 40 | + return $this->hasMany(self::class, 'nhom_me', 'ma_nhvt'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the parent Category. |
| 45 | + */ |
| 46 | + public function catParent(): BelongsTo |
| 47 | + { |
| 48 | + return $this->belongsTo(self::class, 'nhom_me', 'ma_nhvt'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Parent scope. |
| 53 | + * |
| 54 | + * @param mixed $query |
| 55 | + */ |
| 56 | + public function scopeIsRoot($query) |
| 57 | + { |
| 58 | + return $this->scopeHasParent($query, ''); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Parent scope. |
| 63 | + * |
| 64 | + * @param mixed $query |
| 65 | + * @param mixed $parent |
| 66 | + */ |
| 67 | + public function scopeHasParent($query, $parent = '') |
| 68 | + { |
| 69 | + if ($parent instanceof self) { |
| 70 | + return $query->where('nhom_me', $parent->sku); |
| 71 | + } |
| 72 | + if (\is_string($parent)) { |
| 73 | + return $query->where('nhom_me', $parent); |
| 74 | + } |
| 75 | + |
| 76 | + return $query; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gọi stored procedure asINGetDMNHVT để lấy dữ Danh sách nhóm vật tư - hàng hóa. |
| 81 | + * |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public static function getAsINGetDMNHVT(array $params): Collection |
| 85 | + { |
| 86 | + return collect(DB::connection((new static())->getConnectionName())->select('EXECUTE asINGetDMNHVT |
| 87 | + @pMa_Cty = :pMa_Cty, |
| 88 | + @pMa_nhvt = :pMa_nhvt, |
| 89 | + @pStruct = :pStruct |
| 90 | + ', [ |
| 91 | + 'pMa_Cty' => $params['pMa_Cty'] ?? '', |
| 92 | + 'pMa_nhvt' => $params['pMa_nhvt'] ?? null, |
| 93 | + 'pStruct' => $params['pStruct'] ?? null, |
| 94 | + ])); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the Simba Category Id. |
| 99 | + */ |
| 100 | + protected function id(): Attribute |
| 101 | + { |
| 102 | + return Attribute::make( |
| 103 | + get: fn (mixed $value, array $attributes) => implode('_', [$this->ma_cty, $this->ma_nhvt]), |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the Simba Category Sku. |
| 109 | + */ |
| 110 | + protected function sku(): Attribute |
| 111 | + { |
| 112 | + return Attribute::make( |
| 113 | + get: fn (mixed $value, array $attributes) => $this->ma_nhvt, |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the Simba status. |
| 119 | + */ |
| 120 | + protected function status(): Attribute |
| 121 | + { |
| 122 | + return Attribute::make( |
| 123 | + get: static fn (mixed $value, array $attributes) => !$attributes['ksd'], |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get the Simba Category parent. |
| 129 | + */ |
| 130 | + protected function parent(): Attribute |
| 131 | + { |
| 132 | + return Attribute::make( |
| 133 | + get: static fn (mixed $value, array $attributes) => $attributes['nhom_me'] ?: '', |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get the Simba Category name. |
| 139 | + */ |
| 140 | + protected function name(): Attribute |
| 141 | + { |
| 142 | + return Attribute::make( |
| 143 | + get: fn (mixed $value, array $attributes) => $this->ten_nhvt, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + protected function Products(): HasMany |
| 148 | + { |
| 149 | + return $this->hasMany(Product::class, 'category', 'sku'); |
| 150 | + } |
| 151 | + |
| 152 | + protected function urlPath(): Attribute |
| 153 | + { |
| 154 | + return Attribute::make( |
| 155 | + get: fn (mixed $value, array $attributes) => $this->catParent ? ($this->catParent->isRoot ? "{$this->urlKey}" : "{$this->catParent->urlPath}/{$this->urlKey}") : '', |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + protected function urlKey(): Attribute |
| 160 | + { |
| 161 | + return Attribute::make( |
| 162 | + get: fn (mixed $value, array $attributes) => $value ?: ($this->isRoot ? '' : Str::of(vn_convert_encoding($this->name))->slug('-')), |
| 163 | + set: static fn (string $value, array $attributes) => strtolower($value), |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments