Export Stock CSV

This commit is contained in:
Raimon Esteve 2016-12-15 10:27:09 +01:00
parent 9bd89cf44a
commit 75e510db17
3 changed files with 95 additions and 12 deletions

View File

@ -1,4 +1,10 @@
Version 4.0.0 - 2016-05-03
* Export Stock CSV
Version 3.8.0 - 2015-11-11
Version 3.6.0 - 2015-09-28
Version 3.4.0 - 2014-11-05
Version 2.6.0 - 2012-10-08

View File

@ -4,12 +4,14 @@
from datetime import datetime
from decimal import Decimal
from creole import creole2html
from io import BytesIO
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, Not, Equal, Or
from trytond.transaction import Transaction
from trytond import backend
from trytond.modules.product_esale.tools import esale_eval
import unicodecsv
__all__ = ['MagentoProductType', 'MagentoAttributeConfigurable',
'TemplateMagentoAttributeConfigurable', 'Template', 'Product']
@ -21,6 +23,7 @@ _MAGENTO_VISIBILITY = {
'all': '4',
}
class MagentoProductType(ModelSQL, ModelView):
'Magento Product Type'
__name__ = 'magento.product.type'
@ -281,3 +284,83 @@ class Product:
websites.append(ext_ref.mgn_id)
vals['websites'] = websites
return vals
@classmethod
def esale_export_csv_magento(cls, shop, products, lang):
Product = Pool().get('product.product')
app = shop.magento_website.magento_app
values, keys = [], set()
for product in products:
vals = Product.magento_export_product_csv(app, product, shop, lang)
for k in vals.keys():
keys.add(k)
values.append(vals)
output = BytesIO()
wr = unicodecsv.DictWriter(output, sorted(list(keys)),
quoting=unicodecsv.QUOTE_ALL, encoding='utf-8')
wr.writeheader()
wr.writerows(values)
return output
@classmethod
def magento_export_product_csv(cls, app, product, shop, lang):
Configuration = Pool().get('product.configuration')
configuration = Configuration(1)
vals = cls.magento_export_product(app, product, shop, lang)
if app.default_lang and (app.default_lang.code == lang):
# remove websites
if vals.get('websites'):
del vals['websites']
# convert list values to string
vals['category_ids'] = ', '.join(str(x) for x in vals.get('categories'))
del vals['categories']
# add quantity - stock
context = Transaction().context
context['shop'] = shop.id # force the current shop
with Transaction().set_context(context):
quantities = shop.get_esale_product_quantity([product])
qty = quantities[product.id]
vals['qty'] = qty
vals['is_in_stock'] = '1' if qty > 0 else '0'
vals['manage_stock'] = '1' if product.esale_manage_stock else '0'
# images
# http://wiki.magmi.org/index.php?title=Image_attributes_processor
image = []
small_image = []
thumbnail = []
for a in product.template.attachments:
if not a.esale_available:
continue
img = '%(exclude)s%(uri)s%(digest)s/%(filename)s::%(label)s' % {
'exclude': '-' if a.esale_exclude else '',
'uri': configuration.esale_media_uri,
'digest': a.digest,
'filename': a.name,
'label': a.description if a.description else product.template.name,
}
if a.esale_base_image:
image.append(img)
if a.esale_small_image:
small_image.append(img)
if a.esale_thumbnail:
thumbnail.append(img)
vals['image'] = ','.join(image)
vals['small_image'] = ','.join(small_image)
vals['thumbnail'] = ','.join(thumbnail)
else:
# storeview
for l in app.languages:
if lang == l.lang.code:
vals['store'] = l.storeview.code
break
return vals

18
shop.py
View File

@ -94,7 +94,7 @@ class SaleShop:
# Group Price
group_price = []
if self.magento_shop_group_prices and product.magento_group_price:
# {'cust_group': '0', 'website_price': '10.0000', 'price': '10.0000',
# {'cust_group': '0', 'website_price': '10.0000', 'price': '10.0000',
# 'website_id': '0', 'price_id': '1', 'all_groups': '0'}
for group_prices in self.magento_shop_group_prices:
context = {
@ -168,13 +168,7 @@ class SaleShop:
app = self.magento_website.magento_app
language = context.get('language')
default_storeview = app.magento_default_storeview
if default_storeview:
for l in app.languages:
if l.storeview.id == default_storeview.id:
language = l.lang.code
break
language = app.default_lang or context.get('language')
for template in templates:
with Product(app.uri, app.username, app.password) as product_api:
@ -199,7 +193,7 @@ class SaleShop:
values = Prod.magento_export_product(app, product, shop=self, lang=language)
prices = self.magento_get_prices(product)
values.update(prices)
if not values.get('tax_class_id'):
for tax in app.magento_taxes:
values['tax_class_id'] = tax.tax_id
@ -239,7 +233,7 @@ class SaleShop:
magento_product_type = 'simple'
else:
magento_product_type = product.template.magento_product_type
ext_ref = MagentoExternalReferential.get_try2mgn(app,
'esale.attribute.group',
template.esale_attribute_group.id)
@ -298,7 +292,7 @@ class SaleShop:
values.update(prices)
mgn_prods = product_api.list({'sku': {'=': code}})
try:
if mgn_prods:
action = 'update'
@ -439,7 +433,7 @@ class SaleShop:
magento_website = ext_ref.mgn_id
product_api.update(code, data, magento_website, identifierType=app.identifier_type)
if self.magento_price_global: # Global price
product_api.update(code, data, identifierType=app.identifier_type)
product_api.update(code, data, identifierType=app.identifier_type)
else:
product_api.update(code, data, identifierType=app.identifier_type)