Add miniderest recommended price

#033273
This commit is contained in:
Raimon Esteve 2018-09-17 18:06:41 +02:00
parent 23496fb045
commit a25e90bd45
8 changed files with 164 additions and 6 deletions

View file

@ -3,9 +3,12 @@
# the full copyright notices and license terms.
from trytond.pool import Pool
from . import rivals
from . import product
def register():
Pool.register(
product.Product,
product.MinderestRecommendedPrice,
rivals.ProductAppRivals,
module='product_rivals_minderest', type_='model')

26
product.py Normal file
View file

@ -0,0 +1,26 @@
# encoding: utf-8
#This file is part product_rivals_minderest module for Tryton.
#The COPYRIGHT file at the top level of this repository contains
#the full copyright notices and license terms.
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta
from trytond.config import config
__all__ = ['Product', 'MinderestRecommendedPrice']
DIGITS = config.getint('product', 'price_decimal', default=4)
class Product:
__name__ = 'product.product'
__metaclass__ = PoolMeta
minderest_recommended_prices = fields.One2Many(
'product.minderest_recommended_price', 'product',
'Minderest Recommended Price')
class MinderestRecommendedPrice(ModelSQL, ModelView):
'Minderest Recommended Price'
__name__ = 'product.minderest_recommended_price'
product = fields.Many2One('product.product', 'Product', required=True)
price = fields.Numeric('Price', digits=(16, DIGITS))

42
product.xml Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0"?>
<!-- This file is part of product_rivals_minderest module for Tryton.
The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<tryton>
<data>
<record model="ir.ui.view" id="minderest_recommended_price_tree">
<field name="model">product.minderest_recommended_price</field>
<field name="type">tree</field>
<field name="name">minderest_recommended_price_tree</field>
</record>
<record model="ir.ui.view" id="minderest_recommended_price_form">
<field name="model">product.minderest_recommended_price</field>
<field name="type">form</field>
<field name="name">minderest_recommended_price_form</field>
</record>
<record model="ir.action.act_window" id="act_minderest_recommended_price_form">
<field name="name">Minderest Recommended Price</field>
<field name="res_model">product.minderest_recommended_price</field>
</record>
<record model="ir.action.act_window.view" id="act_minderest_recommended_price_form_view1">
<field name="sequence" eval="10"/>
<field name="view" ref="minderest_recommended_price_tree"/>
<field name="act_window" ref="act_minderest_recommended_price_form"/>
</record>
<record model="ir.action.act_window.view" id="act_minderest_recommended_price_form_view2">
<field name="sequence" eval="20"/>
<field name="view" ref="minderest_recommended_price_form"/>
<field name="act_window" ref="act_minderest_recommended_price_form"/>
</record>
<menuitem parent="product_rivals.menu_product_rivals_form" sequence="1"
action="act_minderest_recommended_price_form" id="menu_minderest_recommended_price_form"/>
</data>
<data depends="product_esale">
<record model="ir.ui.view" id="esale_product_view_form">
<field name="model">product.product</field>
<field name="inherit" ref="product_esale.esale_product_view_form"/>
<field name="name">esale_product_form</field>
</record>
</data>
</tryton>

View file

@ -5,14 +5,11 @@ import urllib2
import base64
import csv
from decimal import Decimal
from trytond.config import config
from trytond.pool import PoolMeta
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
__all__ = ['ProductAppRivals']
DIGITS = config.getint('product', 'price_decimal', default=4)
class ProductAppRivals:
__name__ = 'product.app.rivals'
@ -21,8 +18,7 @@ class ProductAppRivals:
@classmethod
def __setup__(cls):
super(ProductAppRivals, cls).__setup__()
required = (Eval('app') == 'minderest')
required = (Eval('app') in ['minderest', 'minderest_recommended_price'])
if 'required' in cls.app_username.states:
cls.app_username.states['required'] |= required
else:
@ -35,6 +31,7 @@ class ProductAppRivals:
@classmethod
def get_app(cls):
res = super(ProductAppRivals, cls).get_app()
res.append(('minderest_recommended_price', 'Minderest Recommended Price'))
res.append(('minderest', 'Minderest'))
return res
@ -89,3 +86,63 @@ class ProductAppRivals:
}
self.create_rivals(values)
def update_prices_minderest_recommended_price(self):
username = self.app_username
password = self.app_password
request = urllib2.Request(self.app_uri)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
rows = result.read().decode('iso-8859-1').encode('utf8')
self.minderest_recommended_prices(rows)
def minderest_recommended_prices(self, rows):
RecommendedPrice = Pool().get('product.minderest_recommended_price')
has_header = True
csv_dict = {}
to_save = []
spamreader = csv.reader(rows.split('\n'), delimiter=';')
field_names_list = spamreader.next()
for row in csv.reader(rows.split('\n'), delimiter=';'):
if not row:
continue
for i in xrange(0,len(field_names_list)):
csv_dict[field_names_list[i]] = row[i]
if has_header:
has_header = False
continue
recommended_price = self.get_recomended_price(csv_dict)
if recommended_price:
to_save.append(recommended_price)
RecommendedPrice.save(to_save)
def recommended_price_domain(self, product):
return [('product', '=', product)]
def get_recomended_price(self, csv_dict):
pool = Pool()
RecommendedPrice = pool.get('product.minderest_recommended_price')
Product = Pool().get('product.product')
products = Product.search([('code', '=', str(csv_dict['g:mpn']))], limit=1)
if not products:
return
product, = products
recommended_prices = RecommendedPrice.search(
self.recommended_price_domain(product), limit=1)
price = Decimal(csv_dict[
'price_recommended_tax_excluded']).quantize(Decimal(str(10**-2)))
if recommended_prices:
recommended_price, = recommended_prices
recommended_price.price = price
else:
recommended_price = RecommendedPrice()
recommended_price.product = product
recommended_price.price = price
return recommended_price

View file

@ -3,3 +3,7 @@ version=4.1.0
depends:
ir
product_rivals
extras_depend:
product_esale
xml:
product.xml

View file

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!-- This file is part product_rivals_minderest module for Tryton.
The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/form/notebook/page[@id='esale']" position="after">
<page string="Minderest Recommended Prices" id="minderest_recommended_prices">
<field name="minderest_recommended_prices" colspan="4"/>
</page>
</xpath>
</data>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<form string="Minderest Recommended Price">
<label name="product"/>
<field name="product"/>
<label name="price"/>
<field name="price"/>
</form>

View file

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tree string="Minderest Recommended Price">
<field name="product"/>
<field name="price"/>
</tree>