예제 #1
0
 /**
  * Create a Price model for a given product
  *
  * @param  Product  $product
  * @return Price
  */
 public function createPrice(Product $product)
 {
     $amount = $this->is_percentage ? $product->base_price * ((100 - $this->amount) / 100) : $product->base_price - $this->amount;
     return Price::create(['product_id' => $product->id, 'discount_id' => $this->id, 'price' => $amount, 'start_at' => $this->start_at, 'end_at' => $this->end_at]);
 }
예제 #2
0
 public function test_products_inherit_discounts_from_their_categories()
 {
     $parent = Factory::create(new Category(), ['is_inheriting' => true]);
     $first = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $first->categories()->attach($parent);
     $child = Factory::create(new Category(), ['parent_id' => $parent->id]);
     $second = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $second->categories()->attach($child);
     $orphan = Factory::create(new Category());
     $third = Factory::create(new Discount(), ['is_percentage' => true, 'amount' => 50]);
     $third->categories()->attach($orphan);
     $product = Factory::create(new Product());
     $product->categories()->sync([$parent->id, $child->id]);
     $product->save();
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($first->id)->count());
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($second->id)->count());
     $this->assertEquals(0, Price::whereProductId($product->id)->whereDiscountId($third->id)->count());
     $product->discounts()->attach($third);
     $product->save();
     $this->assertEquals(1, Price::whereProductId($product->id)->whereDiscountId($third->id)->count());
 }
예제 #3
0
 public function test_discount_inheritance_is_updated_when_the_tree_is_updated()
 {
     $product = Factory::create(new Product());
     $parent = Factory::create(new Category(), ['is_inheriting' => true]);
     $child = Factory::create(new Category(), ['is_inheriting' => true]);
     $grandchild = Factory::create(new Category(), ['is_inheriting' => true]);
     $grandchild->products()->attach($product);
     $discount = Factory::create(new Discount());
     $discount->categories()->attach($parent);
     $discount->save();
     $this->assertEquals(0, Price::where('discount_id', $discount->id)->count());
     $tree = [['id' => $parent->id, 'parent_id' => null], ['id' => $child->id, 'parent_id' => $parent->id], ['id' => $grandchild->id, 'parent_id' => $child->id]];
     Category::updateInheritanceTree($tree);
     $this->assertEquals(1, Price::where('discount_id', $discount->id)->count());
 }
예제 #4
0
 public function test_prices_for_products_of_descendent_categories_added_to_discounts()
 {
     $product = Factory::create(new Product());
     $parent = Factory::create(new Category(), ['is_inheriting' => true]);
     $child = Factory::create(new Category(), ['parent_id' => $parent->id]);
     $child->products()->attach($product);
     $discount = Factory::create(new Discount());
     $discount->categories()->add($parent);
     $discount->save();
     $this->assertEquals(1, Price::where('product_id', $product->id)->where('discount_id', $discount->id)->count());
 }
예제 #5
0
 /**
  * Make sure the product inherits appropriate discounts from it's categories
  *
  * @return void
  */
 public function syncPrices()
 {
     // First, Sync the base price
     $base = Price::getBasePrice($this);
     $base->price = $this->base_price;
     $base->save();
     // Second, clear discounted prices
     $this->prices()->isDiscounted()->delete();
     // Third, recalculate category discounts
     $categories = $this->categories()->with(['discounts' => function ($discount) {
         return $discount->isRunningOrUpcoming();
     }])->get();
     foreach ($categories as $category) {
         foreach ($category->discounts as $discount) {
             $discount->createPrice($this);
         }
     }
     // Finally, recalculate direct discounts
     foreach ($this->discounts()->get() as $discount) {
         $discount->createPrice($this);
     }
 }