relatedItems()->count() > 0; } public function itemCategory() { return $this->belongsTo(ItemCategory::class); } public function itemSaleInfo() { return $this->hasOne(ItemSaleInfo::class); } public function itemPurchaseInfo() { return $this->hasOne(ItemPurchaseInfo::class); } public function priceLists() { return $this->belongsToMany(PriceList::class) ->withPivot('price'); } public function warehouses() { return $this->belongsToMany(Warehouse::class) ->using(ItemWarehouse::class) ->withPivot('initial_stock'); } /** * Transaksi bundle pada item * sebagai "induk" item * * @return void */ public function bundlings() { return $this->hasMany(Bundle::class); } /** * Transaksi bundle pada item * sebagai "component" item * * @return BelongsToMany */ public function bundles() { return $this->belongsToMany(Bundle::class) ->withPivot('qty', 'from_warehouse_id'); } public function relatedItems() { return $this->belongsToMany(Item::class, 'related_item', 'item_id', 'related_item_id') ->using(RelatedItem::class) ->withPivot('qty'); } public function parentItems() { return $this->belongsToMany(Item::class, 'related_item', 'related_item_id', 'item_id') ->using(RelatedItem::class) ->withPivot('qty'); } /** * Transaksi adjustment pada item * * @return BelongsToMany */ public function adjustments() { return $this->belongsToMany(Adjustment::class) ->withPivot('qty_adjusted'); } /** * Transaksi transfer order pada item * * @return BelongsToMany */ public function transferOrders() { return $this->belongsToMany(TransferOrder::class) ->withPivot('qty_transfer'); } /** * Transaksi sale order pada item * * @return BelongsToMany */ public function saleOrders() { return $this->belongsToMany(SaleOrder::class) ->withPivot('qty'); } /** * Transaksi purchase order pada item * * @return BelongsToMany */ public function purchaseOrders() { return $this->belongsToMany(PurchaseOrder::class) ->withPivot('qty'); } public function scopeSearch($q, $keyword) { return $q->where(function ($q) use ($keyword) { $q->where( 'name', 'like', "%$keyword%" )->orWhere( 'sku', 'like', "%$keyword%" ); }); } public function scopeSearchFromCategory($q, $keyword) { return $q->search($keyword) // mencari di data category ->orWhereHas('itemCategory', function ($q) use ($keyword) { return $q->search($keyword); }); } public function scopeIsComposite($q) { return $q->whereHas('relatedItems'); } }