AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BundleItem.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * One thing inside a bundle: a product (and, for a product with options, one of its variants) and how many.
  7. *
  8. * @property int $id
  9. * @property int $store_id
  10. * @property int $bundle_id
  11. * @property int $product_id
  12. * @property int|null $variant_id
  13. * @property int $quantity
  14. * @property int $position
  15. * @property-read Product $bundle
  16. * @property-read Product $product
  17. * @property-read ProductVariant|null $variant
  18. */
  19. class BundleItem extends Model
  20. {
  21. /**
  22. * @return BelongsTo<Product, $this>
  23. */
  24. public function bundle(): BelongsTo
  25. {
  26. return $this->belongsTo(Product::class, 'bundle_id');
  27. }
  28. /**
  29. * @return BelongsTo<Product, $this>
  30. */
  31. public function product(): BelongsTo
  32. {
  33. return $this->belongsTo(Product::class);
  34. }
  35. /**
  36. * @return BelongsTo<ProductVariant, $this>
  37. */
  38. public function variant(): BelongsTo
  39. {
  40. return $this->belongsTo(ProductVariant::class, 'variant_id');
  41. }
  42. /**
  43. * Make a bundle hold exactly these items, in this order. Not a bundle: pass an empty list.
  44. *
  45. * @param list<array{product_id: int, variant_id: int|null, quantity: int}> $items
  46. */
  47. public static function replaceFor(Product $bundle, array $items): void
  48. {
  49. $bundle->bundleItems()->delete();
  50. foreach ($items as $position => $item) {
  51. $row = new self;
  52. $row->store_id = $bundle->store_id;
  53. $row->bundle_id = $bundle->id;
  54. $row->product_id = $item['product_id'];
  55. $row->variant_id = $item['variant_id'];
  56. $row->quantity = $item['quantity'];
  57. $row->position = $position;
  58. $row->save();
  59. }
  60. $bundle->unsetRelation('bundleItems');
  61. }
  62. /**
  63. * "Sambal Hijau (250g)".
  64. */
  65. public function name(): string
  66. {
  67. return $this->product->name.($this->variant !== null ? ' ('.$this->variant->name().')' : '');
  68. }
  69. /**
  70. * "2 × Sambal Hijau (250g)".
  71. */
  72. public function label(): string
  73. {
  74. return $this->quantity.' × '.$this->name();
  75. }
  76. /**
  77. * How many of this item are left: the variant's stock, or the product's.
  78. */
  79. public function stock(): int
  80. {
  81. return $this->variant !== null ? $this->variant->stock : $this->product->stock;
  82. }
  83. /**
  84. * @return array<string, string>
  85. */
  86. protected function casts(): array
  87. {
  88. return [
  89. 'quantity' => 'integer',
  90. 'position' => 'integer',
  91. ];
  92. }
  93. }