| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Models;
- use App\Models\Scopes\YearScope;
- use Illuminate\Database\Eloquent\Attributes\ScopedBy;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- #[ScopedBy([YearScope::class])]
- class OrderView extends Model
- {
- const DEFAULT_SORT_BY = 'created_at';
- protected $table = 'orders_view';
- protected $fillable = [
- 'year',
- 'name',
- 'user_id',
- 'district_id',
- 'area_id',
- 'object_address',
- 'object_type_id',
- 'comment',
- 'installation_date',
- 'ready_date',
- 'brigadier_id',
- 'order_status_id',
- 'tg_group_name',
- 'tg_group_link',
- 'ready_to_mount',
- 'user_name',
- 'district_name',
- 'area_name',
- 'object_type_name',
- 'brigadier_name',
- 'order_status_name',
- ];
- public $appends = ['common_name', 'products_with_count'];
- public function commonName(): Attribute
- {
- return Attribute::make(
- get: fn($value) => (string) $this->object_address . ', ' . $this->area_name . ', ' . $this->district_name,
- );
- }
- public function productsWithCount(): Attribute
- {
- $products = ProductSKU::query()->where('order_id', $this->id)->get(); //$this->products_sku;
- $ret = [];
- foreach ($products as $product) {
- if(isset($ret[$product->product->id])) {
- $ret[$product->product->id]['count'] += 1;
- } else {
- $ret[$product->product->id] = [
- 'name' => $product->product->article,
- 'count' => 1,
- ];
- }
- if($product->maf_order?->order_number) {
- $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
- }
- }
- $s = '';
- $openTag = '<div>';
- $closeTag = '</div>';
- foreach ($ret as $product) {
- $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
- $s .= $openTag . $product['name'] . ' - ' . $product['count'] . $order_numbers . $closeTag;
- }
- return Attribute::make(
- get: fn($value) => (string) $s,
- );
- }
- }
|