SaleDetail.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Models;
  3. use App\Services\HelperService;
  4. use Illuminate\Database\Eloquent\Casts\Attribute;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. class SaleDetail extends Model
  8. {
  9. use HasFactory;
  10. protected $fillable = ["price", "qty", "sale_number", "product_number"];
  11. protected function price(): Attribute
  12. {
  13. return Attribute::make(
  14. get: function ($value) {
  15. $ppn = Ppn::first()->ppn;
  16. return $this->sale->ppn
  17. ? HelperService::ppn($value, $ppn) * $this->qty
  18. : $value * $this->qty;
  19. }
  20. );
  21. }
  22. public function product()
  23. {
  24. return $this->belongsTo(Product::class, "product_number", "number");
  25. }
  26. public function sale()
  27. {
  28. return $this->belongsTo(Sale::class, "sale_number", "number");
  29. }
  30. public function scopeHistoryProductSale($query, array $filters)
  31. {
  32. $query
  33. ->when($filters["productNumber"] ?? null, function (
  34. $query,
  35. $search
  36. ) {
  37. $query->where("product_number", $search);
  38. })
  39. ->when($filters["customerId"] ?? null, function ($query, $search) {
  40. $query->whereHas("sale", function ($query) use ($search) {
  41. $query->where("customer_id", $search);
  42. });
  43. });
  44. }
  45. }