Skip to content

Commit

Permalink
Merge pull request #98 from priyankarani/7718
Browse files Browse the repository at this point in the history
Export Tier Prices To Magento #7718
  • Loading branch information
tarunbhardwaj committed May 7, 2015
2 parents 8c5788b + b1d3d5e commit d5ba41d
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
55 changes: 55 additions & 0 deletions channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,61 @@ def export_inventory_to_magento(self):

return products

@classmethod
def export_tier_prices_to_magento_using_cron(cls):
"""
Export tier prices to magento using cron
"""
channels = cls.search([('source', '=', 'magento')])

for channel in channels:
channel.export_tier_prices_to_magento()

def export_tier_prices_to_magento(self):
"""
Exports tier prices of products from tryton to magento for this channel
:return: List of products
"""
self.validate_magento_channel()

for listing in self.product_listings:

# Get the price tiers from the product listing if the list has
# price tiers else get the default price tiers from current
# channel
price_tiers = listing.price_tiers or self.magento_price_tiers

price_data = []
for tier in price_tiers:
if hasattr(tier, 'product_listing'):
# The price tier comes from a product listing, then it has a
# function field for price, we use it directly
price = tier.price
else:
# The price tier comes from the default tiers on
# channel,
# we dont have a product on tier, so we use the current
# product in loop for computing the price for this tier
price = self.price_list.compute(
None, listing.product, listing.product.list_price,
tier.quantity, self.default_uom
)

price_data.append({
'qty': tier.quantity,
'price': float(price),
})

# Update stock information to magento
with magento.ProductTierPrice(
self.magento_url, self.magento_api_user, self.magento_api_key
) as tier_price_api:
tier_price_api.update(
listing.product_identifier, price_data
)

return len(self.product_listings)


class MagentoTier(ModelSQL, ModelView):
"""Price Tiers for store
Expand Down
13 changes: 13 additions & 0 deletions channel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,18 @@
<field name="function">import_magento_orders</field>
</record>

<!-- Cron To Export Tier Prices To Magento-->
<record model="ir.cron" id="ir_cron_export_tier_prices_to_magento">
<field name="name">Export Tier Prices To Magento</field>
<field name="request_user" ref="res.user_admin"/>
<field name="user" ref="res.user_trigger"/>
<field name="active" eval="True"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="number_calls">-1</field>
<field name="model">sale.channel</field>
<field name="function">export_tier_prices_to_magento_using_cron</field>
</record>

</data>
</tryton>
15 changes: 9 additions & 6 deletions product.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class ProductSaleChannelListing:
__name__ = 'product.product.channel_listing'

price_tiers = fields.One2Many(
'product.price_tier', 'template', 'Price Tiers'
'product.price_tier', 'product_listing', 'Price Tiers'
)
magento_product_type = fields.Selection([
(None, ''),
Expand Down Expand Up @@ -529,8 +529,9 @@ class ProductPriceTier(ModelSQL, ModelView):
__name__ = 'product.price_tier'
_rec_name = 'quantity'

template = fields.Many2One(
'product.template', 'Product Template', required=True, readonly=True,
product_listing = fields.Many2One(
'product.product.channel_listing', 'Product Listing', required=True,
readonly=True,
)
quantity = fields.Float(
'Quantity', required=True
Expand All @@ -545,8 +546,9 @@ def __setup__(cls):
super(ProductPriceTier, cls).__setup__()
cls._sql_constraints += [
(
'template_quantity_unique', 'UNIQUE(template, quantity)',
'Quantity in price tiers must be unique for a product'
'product_listing_quantity_unique',
'UNIQUE(product_listing, quantity)',
'Quantity in price tiers must be unique for a product listing'
)
]

Expand All @@ -562,7 +564,8 @@ def get_price(self, name):

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
product = self.product_listing.product
return channel.price_list.compute(
None, self.template, self.template.list_price, self.quantity,
None, product, product.list_price, self.quantity,
channel.default_uom
)
8 changes: 7 additions & 1 deletion product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ this repository contains the full copyright notices and license terms. -->
<tryton>
<data>

<record model="ir.ui.view" id="product_sale_channel_listing_form">
<field name="model">product.product.channel_listing</field>
<field name="inherit" ref="sale_channel.product_sale_channel_listing_form"/>
<field name="name">product_channel_listing_form</field>
</record>

<record id="product_category_magento_unclassified" model="product.category">
<field name="name">Unclassified Magento Products</field>
</record>

</data>
</data>
</tryton>
15 changes: 9 additions & 6 deletions tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,9 @@ def test_0090_tier_prices(self):
Category.create_using_magento_data(category_data)

product_data = load_json('products', '135')
product = \
Product.find_or_create_using_magento_data(
product_data
)
product = Product.find_or_create_using_magento_data(
product_data
)

price_list, = PriceList.create([{
'name': 'Test Pricelist',
Expand All @@ -439,13 +438,17 @@ def test_0090_tier_prices(self):
self.channel1.price_list = price_list
self.channel1.save()

self.assertEqual(len(product.channel_listings), 1)

listing = product.channel_listings[0]

tier, = ProductPriceTier.create([{
'template': product.id,
'product_listing': listing.id,
'quantity': 10,
}])

self.assertEqual(
product.list_price * Decimal('0.9'), tier.price
listing.product.list_price * Decimal('0.9'), tier.price
)

def test_0110_export_catalog(self):
Expand Down
2 changes: 1 addition & 1 deletion view/export_tier_prices_start_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
this repository contains the full copyright notices and license terms. -->
<form string="Export Tier Prices" col="2">
<image name="tryton-dialog-information" xexpand="0" xfill="0"/>
<label string="This wizard will export product tier prices to magento for this store."
<label string="This wizard will export product tier prices to magento for current magento channel."
id="choose" yalign="0.0" xalign="0.0" xexpand="1"/>
</form>
15 changes: 15 additions & 0 deletions view/product_channel_listing_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<data>
<xpath expr="//field[@name='product_identifier']" position="after">
<label name="magento_product_type"/>
<field name="magento_product_type"/>
</xpath>

<xpath expr="//field[@name='product']" position="after">
<notebook>
<page id="price_tiers" string="Price Tiers">
<field name="price_tiers"/>
</page>
</notebook>
</xpath>
</data>
7 changes: 2 additions & 5 deletions wizard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
"""
magento_
Magento
wizard
:copyright: (c) 2015 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
Expand Down Expand Up @@ -311,8 +309,7 @@ def default_export_(self, fields):
channel.validate_magento_channel()

return {
# TODO: Need to be implemented first
# 'products_count': channel.export_tier_prices_to_magento()
'products_count': channel.export_tier_prices_to_magento()
}


Expand Down

0 comments on commit d5ba41d

Please sign in to comment.