StockProduct.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Ppn;
  4. use App\Services\FunctionService;
  5. use Carbon\Carbon;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. class StockProduct extends Model
  10. {
  11. use HasFactory;
  12. protected $fillable = ["price", "qty", "ppn", "product_number"];
  13. protected function updatedAt(): Attribute
  14. {
  15. return Attribute::make(
  16. get: fn($value) => Carbon::parse($value)->translatedFormat(
  17. "l d/m/y"
  18. )
  19. );
  20. }
  21. protected function price(): Attribute
  22. {
  23. return Attribute::make(
  24. get: function ($value) {
  25. $ppn = Ppn::first()->ppn;
  26. return $this->ppn ? FunctionService::ppn($value, $ppn) : $value;
  27. }
  28. );
  29. }
  30. public function product()
  31. {
  32. return $this->belongsTo(Product::class, "product_number", "number");
  33. }
  34. public function scopeSearch($query, array $filters)
  35. {
  36. $query
  37. ->when($filters["search"] ?? null, function ($query, $search) {
  38. $query->whereHas("product", function ($query) use ($search) {
  39. $query
  40. ->where("number", "like", "%" . $search . "%")
  41. ->orWhere("name", "like", "%" . $search . "%");
  42. });
  43. })
  44. ->where("qty", ">", 0);
  45. }
  46. }