HistoryStockProduct.php 1.7KB

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