Ejemplo n.º 1
0
 public function testCart()
 {
     $product1 = Product::find(1);
     $product1->price = 500;
     $product1->save();
     $product2 = Product::find(2);
     $product2->price = 1000;
     $product2->save();
     //tax=23+13=33%;
     $product1 = $this->catalogService->getCartItem(1);
     //MacBook price:500
     $this->assertNotNull($product1);
     $this->assertEquals(500, $product1->price);
     $this->assertEquals(665, $product1->price_including_tax);
     $product2 = $this->catalogService->getCartItem(2);
     //MacBook price:1000
     $this->assertNotNull($product2);
     $this->assertEquals(1000, $product2->price);
     $this->assertEquals(1330, $product2->price_including_tax);
     //Add 2 products
     $cart = new Cart(Cart::SHOPPING_CART);
     $cart->add(1, $product1);
     $cart->add(1, $product2);
     $this->assertEquals(1500, $cart->getTotal());
     $this->assertEquals(495, $cart->getTax());
     $this->assertEquals(1995, $cart->getTotalIncludingTax());
     //print
     echo json_encode($cart, JSON_PRETTY_PRINT);
     //Add another 2 quantities
     $cart->add(2, $product1);
     $this->assertEquals(2500, $cart->getTotal());
     $this->assertEquals(825, $cart->getTax());
     $this->assertEquals(3325, $cart->getTotalIncludingTax());
     //Remove 2 quantities
     $cart->update(1, $product1->id);
     $this->assertEquals(1500, $cart->getTotal());
     $this->assertEquals(495, $cart->getTax());
     $this->assertEquals(1995, $cart->getTotalIncludingTax());
     //dummy remove
     $cart->remove(111);
     $cart->remove(222);
     //Remove first
     $cart->remove($product1->id);
     $this->assertEquals(1000, $cart->getTotal());
     $this->assertEquals(330, $cart->getTax());
     $this->assertEquals(1330, $cart->getTotalIncludingTax());
     //Remove second
     $cart->remove($product2->id);
     $this->assertEquals(0, $cart->getTotal());
     $this->assertEquals(0, $cart->getTax());
     $this->assertEquals(0, $cart->getTotalIncludingTax());
     echo json_encode($cart, JSON_PRETTY_PRINT);
     $cart = unserialize(serialize($cart));
     $this->assertEquals(0, $cart->getTotal());
     $this->assertEquals(0, $cart->getTax());
     $this->assertEquals(0, $cart->getTotalIncludingTax());
 }
 public function testGetById()
 {
     $product1 = Product::with("description.language")->find(1);
     //MacBook price:500
     $this->assertNotNull($product1);
     $this->assertEquals(500, $product1->price);
     $product2 = Product::find(44);
     //MacBook price:1000
     $this->assertNotNull($product2);
     $this->assertEquals(1000, $product2->price);
 }
 public function getProducts($category_id, $language_id, $params)
 {
     $paging = array_get($params, 'paging', []);
     $page_index = 1;
     $page_size = 10;
     if (count($paging) > 0) {
         $page_index = count($paging) == 2 ? $paging[0] : 1;
         $page_size = count($paging) == 2 ? $paging[1] : $paging[0];
     }
     // force current page to 5
     //Paginator::setCurrentPage($page_index);
     Paginator::currentPageResolver(function () use($page_index) {
         return $page_index;
     });
     $query = Product::select('id', 'price', 'tax_class_id', 'price', 'image', 'views', 'pd.name as name', 'pd.description as description')->join('products_to_categories as pc', function ($join) use($category_id) {
         $join->on('products.id', '=', 'pc.product_id');
         $join->where('pc.category_id', '=', $category_id);
     })->leftJoin('products_descriptions AS pd', function ($join) use($language_id) {
         $join->on('products.id', '=', 'pd.product_id');
         $join->where('pd.language_id', '=', Context::getLanguageId());
     });
     $sorting = array_get($params, 'sorting', []);
     if (count($sorting) > 0) {
         $tokens = explode(":", $sorting[0]);
         $sort_column = $tokens[0];
         $sort_direction = count($tokens) > 1 ? $tokens[1] : 'asc';
         switch ($sort_column) {
             case 'price':
                 $query->orderBy('price', $sort_direction);
                 break;
             case 'popular':
                 $query->orderBy('views', $sort_direction);
                 break;
             case 'newest':
                 $query->orderBy('created_at', $sort_direction);
                 break;
         }
     }
     $items = $query->paginate($page_size);
     foreach ($items as $item) {
         $this->calculateTax($item);
     }
     return $items;
 }