def save_value(self, item): value = self.new_values[item] info = self._get_category_info(item.sellable) if not info: info = ClientCategoryPrice( sellable_id=item.sellable.id, category=self.category, price=value, store=item.store ) else: info.price = value
def save_value(self, item): value = self.new_values[item] info = self._get_category_info(item.sellable) if not info: info = ClientCategoryPrice(sellable_id=item.sellable.id, category=self.category, price=value, store=item.store) else: info.price = value
def save_changes(self): store = self.store for cat, value in self._new_prices.items(): info = store.find(ClientCategoryPrice, sellable_id=self.id, category=cat).one() if not info: info = ClientCategoryPrice( sellable_id=self.id, category=cat, max_discount=self.max_discount, price=value, store=store ) else: info.price = value
def test_markup(self): sellable = Sellable(category=None, cost=0, store=self.store) cat = self.create_client_category(u'Cat 1') cat_price = ClientCategoryPrice(sellable=sellable, category=cat, price=150, max_discount=0, store=self.store) self.assertEqual(cat_price.markup, 0) sellable.cost = 10 self.assertEqual(cat_price.markup, 1400) cat_price.markup = 10
def save_changes(self): store = self.store for cat, value in self._new_prices.items(): info = store.find(ClientCategoryPrice, sellable_id=self.id, category=cat).one() if not info: info = ClientCategoryPrice(sellable_id=self.id, category=cat, max_discount=self.max_discount, price=value, store=store) else: info.price = value
def create_client_category_price(self, category=None, sellable=None, price=None): from stoqlib.domain.sellable import ClientCategoryPrice return ClientCategoryPrice(sellable=sellable or self.create_sellable(price=50), category=category or self.create_client_category(), price=price or 100, store=self.store)
def test_remove(self): # Remove category price and sellable sellable = self.create_sellable() Storable(product=sellable.product, store=self.store) for i in range(10): image = self.create_image() image.sellable = sellable ClientCategoryPrice(sellable=sellable, category=self.create_client_category(), price=100, store=self.store) total = self.store.find(ClientCategoryPrice, sellable=sellable.id).count() total_sellable = self.store.find(Sellable, id=sellable.id).count() total_images = self.store.find(Image, sellable_id=sellable.id).count() self.assertEqual(total, 1) self.assertEqual(total_sellable, 1) self.assertEqual(total_images, 10) sellable.remove() total = self.store.find(ClientCategoryPrice, sellable=sellable.id).count() total_sellable = self.store.find(Sellable, id=sellable.id).count() total_images = self.store.find(Image, sellable_id=sellable.id).count() self.assertEqual(total, 0) self.assertEqual(total_sellable, 0) self.assertEqual(total_images, 0)
def test_default_category_price(self): sellable = self.create_sellable(price=100) category = self.create_client_category(u'Cat 1') ClientCategoryPrice(sellable=sellable, category=category, price=155, store=self.store) self.assertEqual(sellable.price, 100) with self.sysparam(DEFAULT_TABLE_PRICE=category): # Setting the parameter DEFAULT_TABLE_PRICE will change the sellable price if that # sellable has a special price for that category self.assertEqual(sellable.price, 155)
def testIsValidPrice(self): sellable = Sellable(category=self._category, cost=50, description=u"Test", price=currency(100), store=self.store) sellable.max_discount = 0 cat = self.create_client_category(u'Cat 1') cat_price = ClientCategoryPrice(sellable=sellable, category=cat, price=150, max_discount=0, store=self.store) # without a category, and max_discount = 0 self.assertFalse(sellable.is_valid_price(0)) self.assertFalse(sellable.is_valid_price(-10)) self.assertFalse(sellable.is_valid_price(99)) self.assertTrue(sellable.is_valid_price(101)) self.assertTrue(sellable.is_valid_price(100)) # without a category, and max_discount = 10% sellable.max_discount = 10 self.assertFalse(sellable.is_valid_price(0)) self.assertFalse(sellable.is_valid_price(-1)) self.assertFalse(sellable.is_valid_price(89)) self.assertTrue(sellable.is_valid_price(90)) self.assertTrue(sellable.is_valid_price(95)) self.assertTrue(sellable.is_valid_price(99)) self.assertTrue(sellable.is_valid_price(101)) # Now with a category, max_discount = 0 self.assertFalse(sellable.is_valid_price(0, cat)) self.assertFalse(sellable.is_valid_price(-10, cat)) self.assertFalse(sellable.is_valid_price(Decimal('149.99'), cat)) self.assertTrue(sellable.is_valid_price(150, cat)) self.assertTrue(sellable.is_valid_price(151, cat)) # Now with a category, max_discount = 10% cat_price.max_discount = 10 self.assertTrue(sellable.is_valid_price(Decimal('149.99'), cat)) self.assertTrue(sellable.is_valid_price(135, cat)) self.assertFalse(sellable.is_valid_price(134, cat))
def test_category_name(self): sellable = Sellable(category=None, cost=50, description=u"Test", price=currency(100), store=self.store) sellable.max_discount = 0 cat = self.create_client_category(u'Cat 1') cat_price = ClientCategoryPrice(sellable=sellable, category=cat, price=150, max_discount=0, store=self.store) self.assertEquals(cat_price.category_name, u'Cat 1')
def test_default_category_price(self): sellable = self.create_sellable(price=100) category = self.create_client_category(u'Cat 1') ClientCategoryPrice(sellable=sellable, category=category, price=155, store=self.store) self.assertEqual(sellable.price, 100) with self.sysparam(DEFAULT_TABLE_PRICE=category): # Setting the parameter DEFAULT_TABLE_PRICE will change the sellable price if that # sellable has a special price for that category self.assertEqual(sellable.price, 155) # When a branch as another category price, that one should be used category = self.create_client_category(u'Cat 2') branch = self.create_branch() branch.default_client_category = category ClientCategoryPrice(sellable=sellable, category=category, price=200, store=self.store) self.assertEqual(sellable.get_price(branch), 200)
def test_remove_with_product(self, warning): category = ClientCategory(name=u'foo', store=self.store) ClientCategoryPrice(category=category, sellable=self.create_sellable(), store=self.store) dialog = ClientCategoryDialog(self.store, reuse_store=True) dialog.list_slave.listcontainer.list.select(category) with mock.patch.object(dialog.list_slave.listcontainer, 'default_remove') as default_remove: default_remove.return_value = True self.click(dialog.list_slave.listcontainer.remove_button) msg = _("%s cannot be deleted, because is used in one or more " "products.") % category.name warning.assert_called_once_with(msg)
def test_category_price(self): sellable = self.create_sellable(price=100) category1 = self.create_client_category(u'Cat 1') category_price = ClientCategoryPrice(sellable=sellable, category=category1, price=155, store=self.store) category2 = self.create_client_category(u'Cat 2') cats = sellable.get_category_prices() self.assertEquals(cats.count(), 1) self.assertTrue(cats[0] == category_price) self.assertEquals(sellable.get_price_for_category(category1), 155) self.assertEquals(sellable.get_price_for_category(category2), 100)
def create_model(self): sellable = self._sellable category = self.target_combo.get_selected_data() if sellable.get_category_price_info(category): product_desc = sellable.get_description() info(_(u'%s already have a price for category %s') % (product_desc, category.get_description())) return model = ClientCategoryPrice(sellable=sellable, category=category, price=sellable.price, max_discount=sellable.max_discount, store=self.store) return model
def test_client_category(self): categories = self.store.find(ClientCategory, name=u'Category') self.assertEquals(categories.count(), 0) category = self.create_client_category(u'Category') categories = self.store.find(ClientCategory, name=u'Category') self.assertEquals(categories.count(), 1) self.assertTrue(category.can_remove()) category.remove() categories = self.store.find(ClientCategory, name=u'Category') self.assertEquals(categories.count(), 0) sellable = self.create_sellable(price=50) category = self.create_client_category(u'Category') ClientCategoryPrice(sellable=sellable, category=category, price=75, store=self.store) self.assertFalse(category.can_remove())
def test_remove(self): # Remove category price and sellable sellable = self.create_sellable() Storable(product=sellable.product, store=self.store) ClientCategoryPrice(sellable=sellable, category=self.create_client_category(), price=100, store=self.store) total = self.store.find(ClientCategoryPrice, sellable=sellable.id).count() total_sellable = self.store.find(Sellable, id=sellable.id).count() self.assertEquals(total, 1) self.assertEquals(total_sellable, 1) sellable.remove() total = self.store.find(ClientCategoryPrice, sellable=sellable.id).count() total_sellable = self.store.find(Sellable, id=sellable.id).count() self.assertEquals(total, 0) self.assertEquals(total_sellable, 0)
def test_is_valid_price(self): def isValidPriceAssert(valid_data, expected_validity, min_price, max_discount): self.assertEquals(valid_data['is_valid'], expected_validity) self.assertEquals(valid_data['min_price'], min_price) self.assertEquals(valid_data['max_discount'], max_discount) sellable = Sellable(category=self._category, cost=50, description=u"Test", price=currency(100), store=self.store) sellable.max_discount = 0 cat = self.create_client_category(u'Cat 1') cat_price = ClientCategoryPrice(sellable=sellable, category=cat, price=150, max_discount=0, store=self.store) user = self.create_user() user.profile.max_discount = 50 # without a category, and max_discount = 0, user = None valid_data = sellable.is_valid_price(-10) isValidPriceAssert(valid_data, False, sellable.price, 0) valid_data = sellable.is_valid_price(0) isValidPriceAssert(valid_data, False, sellable.price, 0) valid_data = sellable.is_valid_price(99) isValidPriceAssert(valid_data, False, sellable.price, 0) valid_data = sellable.is_valid_price(100) isValidPriceAssert(valid_data, True, sellable.price, 0) valid_data = sellable.is_valid_price(101) isValidPriceAssert(valid_data, True, sellable.price, 0) # without a category, and max_discount = 10% sellable.max_discount = 10 valid_data = sellable.is_valid_price(-1) isValidPriceAssert(valid_data, False, currency(90), 10) valid_data = sellable.is_valid_price(0) isValidPriceAssert(valid_data, False, currency(90), 10) valid_data = sellable.is_valid_price(89) isValidPriceAssert(valid_data, False, currency(90), 10) valid_data = sellable.is_valid_price(90) isValidPriceAssert(valid_data, True, currency(90), 10) valid_data = sellable.is_valid_price(91) isValidPriceAssert(valid_data, True, currency(90), 10) # Now with a category, max_discount = 0 valid_data = sellable.is_valid_price(0, cat) isValidPriceAssert(valid_data, False, currency(150), 0) valid_data = sellable.is_valid_price(-10, cat) isValidPriceAssert(valid_data, False, currency(150), 0) valid_data = sellable.is_valid_price(Decimal('149.99'), cat) isValidPriceAssert(valid_data, False, currency(150), 0) valid_data = sellable.is_valid_price(150, cat) isValidPriceAssert(valid_data, True, currency(150), 0) valid_data = sellable.is_valid_price(151, cat) isValidPriceAssert(valid_data, True, currency(150), 0) # Now with a category, max_discount = 10% cat_price.max_discount = 10 valid_data = sellable.is_valid_price(Decimal('149.99'), cat) isValidPriceAssert(valid_data, True, currency(135), 10) valid_data = sellable.is_valid_price(135, cat) isValidPriceAssert(valid_data, True, currency(135), 10) valid_data = sellable.is_valid_price(134, cat) isValidPriceAssert(valid_data, False, currency(135), 10) # with a user valid_data = sellable.is_valid_price(49, None, user) isValidPriceAssert(valid_data, False, currency(50), 50) valid_data = sellable.is_valid_price(50, None, user) isValidPriceAssert(valid_data, True, currency(50), 50)