2013-06-28 16:28:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-01-06 13:57:27 +00:00
|
|
|
import werkzeug
|
2013-10-30 13:30:19 +00:00
|
|
|
|
2013-08-13 12:43:06 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2014-07-06 14:44:26 +00:00
|
|
|
from openerp import http
|
|
|
|
from openerp.http import request
|
2014-02-06 15:47:11 +00:00
|
|
|
from openerp.tools.translate import _
|
2014-05-05 09:57:24 +00:00
|
|
|
from openerp.addons.website.models.website import slug
|
2013-06-28 16:28:04 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
PPG = 20 # Products Per Page
|
|
|
|
PPR = 4 # Products Per Row
|
2013-11-13 14:00:09 +00:00
|
|
|
|
2013-12-01 00:35:20 +00:00
|
|
|
class table_compute(object):
|
2013-12-01 00:23:55 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
|
|
|
|
|
|
|
def _check_place(self, posx, posy, sizex, sizey):
|
2014-03-07 16:52:39 +00:00
|
|
|
res = True
|
2013-12-01 00:23:55 +00:00
|
|
|
for y in range(sizey):
|
|
|
|
for x in range(sizex):
|
|
|
|
if posx+x>=PPR:
|
2014-03-07 16:52:39 +00:00
|
|
|
res = False
|
|
|
|
break
|
2013-12-01 00:23:55 +00:00
|
|
|
row = self.table.setdefault(posy+y, {})
|
|
|
|
if row.setdefault(posx+x) is not None:
|
2014-03-07 16:52:39 +00:00
|
|
|
res = False
|
|
|
|
break
|
|
|
|
for x in range(PPR):
|
|
|
|
self.table[posy+y].setdefault(x, None)
|
|
|
|
return res
|
2013-12-01 00:23:55 +00:00
|
|
|
|
|
|
|
def process(self, products):
|
2013-12-01 00:35:20 +00:00
|
|
|
# Compute products positions on the grid
|
2013-12-01 00:23:55 +00:00
|
|
|
minpos = 0
|
|
|
|
index = 0
|
2013-12-02 09:42:29 +00:00
|
|
|
maxy = 0
|
2013-12-01 00:23:55 +00:00
|
|
|
for p in products:
|
2014-05-27 14:11:27 +00:00
|
|
|
x = min(max(p.website_size_x, 1), PPR)
|
|
|
|
y = min(max(p.website_size_y, 1), PPR)
|
2013-12-01 00:23:55 +00:00
|
|
|
if index>PPG:
|
|
|
|
x = y = 1
|
2013-12-02 09:42:29 +00:00
|
|
|
|
2013-12-01 00:23:55 +00:00
|
|
|
pos = minpos
|
|
|
|
while not self._check_place(pos%PPR, pos/PPR, x, y):
|
|
|
|
pos += 1
|
2013-12-02 09:42:29 +00:00
|
|
|
|
|
|
|
if index>PPG and (pos/PPR)>maxy:
|
|
|
|
break
|
|
|
|
|
2013-12-01 00:35:20 +00:00
|
|
|
if x==1 and y==1: # simple heuristic for CPU optimization
|
2013-12-01 00:23:55 +00:00
|
|
|
minpos = pos/PPR
|
|
|
|
|
|
|
|
for y2 in range(y):
|
|
|
|
for x2 in range(x):
|
|
|
|
self.table[(pos/PPR)+y2][(pos%PPR)+x2] = False
|
2013-12-02 09:42:29 +00:00
|
|
|
self.table[pos/PPR][pos%PPR] = {
|
2013-12-01 00:23:55 +00:00
|
|
|
'product': p, 'x':x, 'y': y,
|
2014-06-16 09:31:21 +00:00
|
|
|
'class': " ".join(map(lambda x: x.html_class or '', p.website_style_ids))
|
2013-12-01 00:23:55 +00:00
|
|
|
}
|
2013-12-02 09:42:29 +00:00
|
|
|
if index<=PPG:
|
|
|
|
maxy=max(maxy,y+(pos/PPR))
|
2013-12-01 00:23:55 +00:00
|
|
|
index += 1
|
|
|
|
|
2013-12-01 00:35:20 +00:00
|
|
|
# Format table according to HTML needs
|
2013-12-01 00:23:55 +00:00
|
|
|
rows = self.table.items()
|
|
|
|
rows.sort()
|
|
|
|
rows = map(lambda x: x[1], rows)
|
|
|
|
for col in range(len(rows)):
|
|
|
|
cols = rows[col].items()
|
|
|
|
cols.sort()
|
2014-03-07 16:52:39 +00:00
|
|
|
x += len(cols)
|
|
|
|
rows[col] = [c for c in map(lambda x: x[1], cols) if c != False]
|
|
|
|
|
2014-03-11 09:30:50 +00:00
|
|
|
return rows
|
2013-12-01 00:23:55 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
# TODO keep with input type hidden
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2013-06-28 16:28:04 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
class QueryURL(object):
|
|
|
|
def __init__(self, path='', **args):
|
|
|
|
self.path = path
|
|
|
|
self.args = args
|
2013-09-26 11:18:54 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
def __call__(self, path=None, **kw):
|
|
|
|
if not path:
|
|
|
|
path = self.path
|
|
|
|
for k,v in self.args.items():
|
|
|
|
kw.setdefault(k,v)
|
|
|
|
l = []
|
|
|
|
for k,v in kw.items():
|
|
|
|
if v:
|
|
|
|
if isinstance(v, list) or isinstance(v, set):
|
|
|
|
l.append(werkzeug.url_encode([(k,i) for i in v]))
|
|
|
|
else:
|
|
|
|
l.append(werkzeug.url_encode([(k,v)]))
|
|
|
|
if l:
|
|
|
|
path += '?' + '&'.join(l)
|
|
|
|
return path
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2014-06-24 13:11:53 +00:00
|
|
|
class website_sale(http.Controller):
|
2013-12-01 00:23:55 +00:00
|
|
|
|
2014-06-25 09:31:39 +00:00
|
|
|
def get_pricelist(self):
|
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
|
|
|
sale_order = context.get('sale_order')
|
|
|
|
if sale_order:
|
|
|
|
pricelist = sale_order.pricelist_id
|
|
|
|
else:
|
|
|
|
partner = pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
|
|
|
|
pricelist = partner.property_product_pricelist
|
|
|
|
return pricelist
|
|
|
|
|
|
|
|
def get_attribute_value_ids(self, product):
|
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
|
|
|
currency_obj = pool['res.currency']
|
|
|
|
attribute_value_ids = []
|
|
|
|
if request.website.pricelist_id.id != context['pricelist']:
|
|
|
|
website_currency_id = request.website.currency_id.id
|
|
|
|
currency_id = self.get_pricelist().currency_id.id
|
|
|
|
for p in product.product_variant_ids:
|
|
|
|
price = currency_obj.compute(cr, uid, website_currency_id, currency_id, p.lst_price)
|
|
|
|
attribute_value_ids.append([p.id, map(int, p.attribute_value_ids), p.price, price])
|
|
|
|
else:
|
|
|
|
attribute_value_ids = [[p.id, map(int, p.attribute_value_ids), p.price, p.lst_price] for p in product.product_variant_ids]
|
|
|
|
|
|
|
|
return attribute_value_ids
|
|
|
|
|
2014-04-10 14:35:24 +00:00
|
|
|
@http.route(['/shop',
|
|
|
|
'/shop/page/<int:page>',
|
|
|
|
'/shop/category/<model("product.public.category"):category>',
|
|
|
|
'/shop/category/<model("product.public.category"):category>/page/<int:page>'
|
2014-05-13 09:35:45 +00:00
|
|
|
], type='http', auth="public", website=True)
|
2014-01-30 23:39:51 +00:00
|
|
|
def shop(self, page=0, category=None, search='', **post):
|
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
|
|
|
|
|
|
|
domain = request.website.sale_product_domain()
|
2013-11-21 14:16:47 +00:00
|
|
|
if search:
|
2014-05-27 07:42:52 +00:00
|
|
|
domain += ['|', '|', '|', ('name', 'ilike', search), ('description', 'ilike', search),
|
|
|
|
('description_sale', 'ilike', search), ('product_variant_ids.default_code', 'ilike', search)]
|
2013-10-15 08:33:31 +00:00
|
|
|
if category:
|
2014-05-27 07:42:52 +00:00
|
|
|
domain += [('product_variant_ids.public_categ_ids', 'child_of', int(category))]
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2014-06-17 07:44:32 +00:00
|
|
|
attrib_list = request.httprequest.args.getlist('attrib')
|
2014-06-17 15:02:03 +00:00
|
|
|
attrib_values = [map(int,v.split("-")) for v in attrib_list if v]
|
2014-06-13 10:31:41 +00:00
|
|
|
attrib_set = set([v[1] for v in attrib_values])
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
if attrib_values:
|
2014-05-27 16:20:42 +00:00
|
|
|
attrib = None
|
|
|
|
ids = []
|
2014-05-27 07:42:52 +00:00
|
|
|
for value in attrib_values:
|
|
|
|
if not attrib:
|
|
|
|
attrib = value[0]
|
2014-05-27 16:20:42 +00:00
|
|
|
ids.append(value[1])
|
2014-05-27 07:42:52 +00:00
|
|
|
elif value[0] == attrib:
|
2014-05-27 16:20:42 +00:00
|
|
|
ids.append(value[1])
|
2014-05-27 07:42:52 +00:00
|
|
|
else:
|
2014-05-28 17:42:15 +00:00
|
|
|
domain += [('attribute_line_ids.value_ids', 'in', ids)]
|
2014-05-27 16:20:42 +00:00
|
|
|
attrib = value[0]
|
|
|
|
ids = [value[1]]
|
2014-05-27 07:42:52 +00:00
|
|
|
if attrib:
|
2014-05-28 17:42:15 +00:00
|
|
|
domain += [('attribute_line_ids.value_ids', 'in', ids)]
|
2014-05-27 07:42:52 +00:00
|
|
|
|
2014-06-17 07:44:32 +00:00
|
|
|
keep = QueryURL('/shop', category=category and int(category), search=search, attrib=attrib_list)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-21 17:28:16 +00:00
|
|
|
if not context.get('pricelist'):
|
2014-07-02 16:18:29 +00:00
|
|
|
pricelist = self.get_pricelist()
|
|
|
|
context['pricelist'] = int(pricelist)
|
|
|
|
else:
|
|
|
|
pricelist = pool.get('product.pricelist').browse(cr, uid, context['pricelist'], context)
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
product_obj = pool.get('product.template')
|
2014-05-27 07:42:52 +00:00
|
|
|
|
2014-08-25 14:19:29 +00:00
|
|
|
url = "/shop"
|
2013-12-01 00:23:55 +00:00
|
|
|
product_count = product_obj.search_count(cr, uid, domain, context=context)
|
2014-08-25 14:19:29 +00:00
|
|
|
if search:
|
|
|
|
post["search"] = search
|
|
|
|
if category:
|
|
|
|
url = "/shop/category/%s" % slug(category)
|
|
|
|
pager = request.website.pager(url=url, total=product_count, page=page, step=PPG, scope=7, url_args=post)
|
2014-01-30 23:39:51 +00:00
|
|
|
product_ids = product_obj.search(cr, uid, domain, limit=PPG+10, offset=pager['offset'], order='website_published desc, website_sequence desc', context=context)
|
|
|
|
products = product_obj.browse(cr, uid, product_ids, context=context)
|
2013-08-06 09:23:44 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
style_obj = pool['product.style']
|
|
|
|
style_ids = style_obj.search(cr, uid, [], context=context)
|
|
|
|
styles = style_obj.browse(cr, uid, style_ids, context=context)
|
2013-09-27 10:31:48 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
category_obj = pool['product.public.category']
|
2013-12-01 17:25:41 +00:00
|
|
|
category_ids = category_obj.search(cr, uid, [], context=context)
|
|
|
|
categories = category_obj.browse(cr, uid, category_ids, context=context)
|
|
|
|
categs = filter(lambda x: not x.parent_id, categories)
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
attributes_obj = request.registry['product.attribute']
|
2014-07-02 16:18:29 +00:00
|
|
|
attributes_ids = attributes_obj.search(cr, uid, [], context=context)
|
|
|
|
attributes = attributes_obj.browse(cr, uid, attributes_ids, context=context)
|
|
|
|
|
|
|
|
from_currency = pool.get('product.price.type')._get_field_currency(cr, uid, 'list_price', context)
|
|
|
|
to_currency = pricelist.currency_id
|
|
|
|
compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2014-01-30 23:39:51 +00:00
|
|
|
'search': search,
|
2014-06-13 14:17:51 +00:00
|
|
|
'category': category,
|
2014-05-27 07:42:52 +00:00
|
|
|
'attrib_values': attrib_values,
|
2014-01-30 23:39:51 +00:00
|
|
|
'attrib_set': attrib_set,
|
|
|
|
'pager': pager,
|
2014-07-02 16:18:29 +00:00
|
|
|
'pricelist': pricelist,
|
2013-12-01 00:23:55 +00:00
|
|
|
'products': products,
|
2013-12-01 00:35:20 +00:00
|
|
|
'bins': table_compute().process(products),
|
2013-12-05 16:22:40 +00:00
|
|
|
'rows': PPR,
|
2013-09-27 10:31:48 +00:00
|
|
|
'styles': styles,
|
2013-12-01 17:25:41 +00:00
|
|
|
'categories': categs,
|
2014-01-30 23:39:51 +00:00
|
|
|
'attributes': attributes,
|
2014-07-02 16:18:29 +00:00
|
|
|
'compute_currency': compute_currency,
|
2014-01-30 23:39:51 +00:00
|
|
|
'keep': keep,
|
2013-12-01 17:25:41 +00:00
|
|
|
'style_in_product': lambda style, product: style.id in [s.id for s in product.website_style_ids],
|
2014-01-30 23:39:51 +00:00
|
|
|
'attrib_encode': lambda attribs: werkzeug.url_encode([('attrib',i) for i in attribs]),
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_sale.products", values)
|
2013-06-28 16:28:04 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/product/<model("product.template"):product>'], type='http', auth="public", website=True)
|
2014-01-30 23:39:51 +00:00
|
|
|
def product(self, product, category='', search='', **kwargs):
|
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
2014-02-21 17:28:16 +00:00
|
|
|
category_obj = pool['product.public.category']
|
2014-06-12 14:58:44 +00:00
|
|
|
template_obj = pool['product.template']
|
2013-09-04 08:49:58 +00:00
|
|
|
|
2014-05-27 07:42:52 +00:00
|
|
|
context.update(active_id=product.id)
|
|
|
|
|
2013-11-08 16:49:17 +00:00
|
|
|
if category:
|
2014-06-19 08:53:30 +00:00
|
|
|
category = category_obj.browse(cr, uid, int(category), context=context)
|
2013-09-22 19:33:56 +00:00
|
|
|
|
2014-06-17 07:44:32 +00:00
|
|
|
attrib_list = request.httprequest.args.getlist('attrib')
|
2014-06-17 15:02:03 +00:00
|
|
|
attrib_values = [map(int,v.split("-")) for v in attrib_list if v]
|
2014-05-27 18:57:00 +00:00
|
|
|
attrib_set = set([v[1] for v in attrib_values])
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-06-17 07:44:32 +00:00
|
|
|
keep = QueryURL('/shop', category=category and category.id, search=search, attrib=attrib_list)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
category_ids = category_obj.search(cr, uid, [], context=context)
|
|
|
|
category_list = category_obj.name_get(cr, uid, category_ids, context=context)
|
|
|
|
category_list = sorted(category_list, key=lambda category: category[1])
|
2013-09-26 13:09:54 +00:00
|
|
|
|
2014-07-02 17:03:10 +00:00
|
|
|
pricelist = self.get_pricelist()
|
|
|
|
|
|
|
|
from_currency = pool.get('product.price.type')._get_field_currency(cr, uid, 'list_price', context)
|
|
|
|
to_currency = pricelist.currency_id
|
|
|
|
compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
|
|
|
|
|
2014-02-21 17:28:16 +00:00
|
|
|
if not context.get('pricelist'):
|
|
|
|
context['pricelist'] = int(self.get_pricelist())
|
2014-06-19 08:53:30 +00:00
|
|
|
product = template_obj.browse(cr, uid, int(product), context=context)
|
2014-02-21 17:28:16 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2014-01-30 23:39:51 +00:00
|
|
|
'search': search,
|
2013-09-22 19:33:56 +00:00
|
|
|
'category': category,
|
2014-07-02 17:03:10 +00:00
|
|
|
'pricelist': pricelist,
|
2014-05-27 18:57:00 +00:00
|
|
|
'attrib_values': attrib_values,
|
2014-07-02 17:03:10 +00:00
|
|
|
'compute_currency': compute_currency,
|
2014-01-30 23:39:51 +00:00
|
|
|
'attrib_set': attrib_set,
|
|
|
|
'keep': keep,
|
2013-09-04 08:49:58 +00:00
|
|
|
'category_list': category_list,
|
2013-10-10 13:29:07 +00:00
|
|
|
'main_object': product,
|
2013-08-19 13:19:29 +00:00
|
|
|
'product': product,
|
2014-06-25 09:31:39 +00:00
|
|
|
'get_attribute_value_ids': self.get_attribute_value_ids
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_sale.product", values)
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
@http.route(['/shop/product/comment/<int:product_template_id>'], type='http', auth="public", methods=['POST'], website=True)
|
2014-01-06 13:57:27 +00:00
|
|
|
def product_comment(self, product_template_id, **post):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
if post.get('comment'):
|
|
|
|
request.registry['product.template'].message_post(
|
|
|
|
cr, uid, product_template_id,
|
|
|
|
body=post.get('comment'),
|
|
|
|
type='comment',
|
|
|
|
subtype='mt_comment',
|
|
|
|
context=dict(context, mail_create_nosubcribe=True))
|
|
|
|
return werkzeug.utils.redirect(request.httprequest.referrer + "#comments")
|
|
|
|
|
2014-06-18 16:19:01 +00:00
|
|
|
@http.route(['/shop/pricelist'], type='http', auth="public", website=True)
|
|
|
|
def pricelist(self, promo, **post):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
request.website.sale_get_order(code=promo, context=context)
|
|
|
|
return request.redirect("/shop/cart")
|
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/cart'], type='http', auth="public", website=True)
|
2014-01-30 23:39:51 +00:00
|
|
|
def cart(self, **post):
|
2014-06-11 10:50:41 +00:00
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
2014-01-30 23:39:51 +00:00
|
|
|
order = request.website.sale_get_order()
|
2014-07-02 17:20:30 +00:00
|
|
|
if order:
|
|
|
|
from_currency = pool.get('product.price.type')._get_field_currency(cr, uid, 'list_price', context)
|
|
|
|
to_currency = order.pricelist_id.currency_id
|
|
|
|
compute_currency = lambda price: pool['res.currency']._compute(cr, uid, from_currency, to_currency, price, context=context)
|
|
|
|
else:
|
|
|
|
compute_currency = lambda price: price
|
2014-07-02 16:18:29 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2014-01-30 23:39:51 +00:00
|
|
|
'order': order,
|
2014-07-02 16:18:29 +00:00
|
|
|
'compute_currency': compute_currency,
|
2014-06-17 07:44:32 +00:00
|
|
|
'suggested_products': [],
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2014-01-30 23:39:51 +00:00
|
|
|
if order:
|
2014-09-10 08:30:25 +00:00
|
|
|
_order = order
|
2014-06-11 10:50:41 +00:00
|
|
|
if not context.get('pricelist'):
|
2014-09-10 08:30:25 +00:00
|
|
|
_order = order.with_context(pricelist=order.pricelist_id.id)
|
|
|
|
values['suggested_products'] = _order._cart_accessories()
|
2014-06-11 10:50:41 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
return request.website.render("website_sale.cart", values)
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/cart/update'], type='http', auth="public", methods=['POST'], website=True)
|
2014-06-25 10:00:27 +00:00
|
|
|
def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2014-06-25 12:03:23 +00:00
|
|
|
request.website.sale_get_order(force_create=1)._cart_update(product_id=int(product_id), add_qty=float(add_qty), set_qty=float(set_qty))
|
2014-06-25 10:00:27 +00:00
|
|
|
return request.redirect("/shop/cart")
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/cart/update_json'], type='json', auth="public", methods=['POST'], website=True)
|
2014-06-12 14:58:44 +00:00
|
|
|
def cart_update_json(self, product_id, line_id, add_qty=None, set_qty=None, display=True):
|
2014-01-30 23:39:51 +00:00
|
|
|
order = request.website.sale_get_order(force_create=1)
|
2014-06-25 09:31:39 +00:00
|
|
|
value = order._cart_update(product_id=product_id, line_id=line_id, add_qty=add_qty, set_qty=set_qty)
|
2014-06-12 14:58:44 +00:00
|
|
|
if not display:
|
|
|
|
return None
|
2014-06-25 09:31:39 +00:00
|
|
|
value['cart_quantity'] = order.cart_quantity
|
|
|
|
value['website_sale.total'] = request.website._render("website_sale.total", {
|
|
|
|
'website_sale_order': request.website.sale_get_order()
|
|
|
|
})
|
|
|
|
return value
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Checkout
|
|
|
|
#------------------------------------------------------
|
2014-02-25 12:57:36 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
def checkout_redirection(self, order):
|
2013-11-13 20:27:32 +00:00
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
2013-09-24 12:42:53 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
# must have a draft sale order with lines at this point, otherwise reset
|
2014-04-10 14:35:24 +00:00
|
|
|
if not order or order.state != 'draft':
|
2014-05-22 13:34:09 +00:00
|
|
|
request.session['sale_order_id'] = None
|
2014-05-22 13:40:29 +00:00
|
|
|
request.session['sale_transaction_id'] = None
|
2014-01-30 23:39:51 +00:00
|
|
|
return request.redirect('/shop')
|
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
# if transaction pending / done: redirect to confirmation
|
|
|
|
tx = context.get('website_sale_transaction')
|
|
|
|
if tx and tx.state != 'draft':
|
|
|
|
return request.redirect('/shop/payment/confirmation/%s' % order.id)
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
def checkout_values(self, data=None):
|
2014-01-30 23:39:51 +00:00
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
2013-11-13 18:43:03 +00:00
|
|
|
orm_partner = registry.get('res.partner')
|
|
|
|
orm_user = registry.get('res.users')
|
|
|
|
orm_country = registry.get('res.country')
|
2014-01-30 23:39:51 +00:00
|
|
|
state_orm = registry.get('res.country.state')
|
|
|
|
|
2013-11-21 14:16:47 +00:00
|
|
|
country_ids = orm_country.search(cr, SUPERUSER_ID, [], context=context)
|
2013-11-13 18:43:03 +00:00
|
|
|
countries = orm_country.browse(cr, SUPERUSER_ID, country_ids, context)
|
2013-11-21 14:16:47 +00:00
|
|
|
states_ids = state_orm.search(cr, SUPERUSER_ID, [], context=context)
|
2013-11-13 18:43:03 +00:00
|
|
|
states = state_orm.browse(cr, SUPERUSER_ID, states_ids, context)
|
2014-08-01 15:11:20 +00:00
|
|
|
partner = orm_user.browse(cr, SUPERUSER_ID, request.uid, context).partner_id
|
2013-07-02 13:29:40 +00:00
|
|
|
|
2014-08-01 15:11:20 +00:00
|
|
|
order = None
|
|
|
|
|
|
|
|
shipping_id = None
|
|
|
|
shipping_ids = []
|
2014-02-24 16:20:41 +00:00
|
|
|
checkout = {}
|
2014-02-25 10:09:34 +00:00
|
|
|
if not data:
|
2014-09-04 11:08:42 +00:00
|
|
|
print request.uid, request.website.user_id.id
|
2014-02-25 10:09:34 +00:00
|
|
|
if request.uid != request.website.user_id.id:
|
|
|
|
checkout.update( self.checkout_parse("billing", partner) )
|
2014-08-01 15:11:20 +00:00
|
|
|
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, [("parent_id", "=", partner.id), ('type', "=", 'delivery')], context=context)
|
2014-02-25 10:09:34 +00:00
|
|
|
else:
|
|
|
|
order = request.website.sale_get_order(force_create=1, context=context)
|
|
|
|
if order.partner_id:
|
2014-09-04 11:08:42 +00:00
|
|
|
domain = [("partner_id", "=", order.partner_id.id)]
|
|
|
|
user_ids = request.registry['res.users'].search(cr, SUPERUSER_ID, domain, context=dict(context or {}, active_test=False))
|
2014-02-25 10:09:34 +00:00
|
|
|
if not user_ids or request.website.user_id.id not in user_ids:
|
|
|
|
checkout.update( self.checkout_parse("billing", order.partner_id) )
|
|
|
|
else:
|
|
|
|
checkout = self.checkout_parse('billing', data)
|
2014-08-01 15:11:20 +00:00
|
|
|
try:
|
|
|
|
shipping_id = int(data["shipping_id"])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if shipping_id == -1:
|
2014-02-25 10:09:34 +00:00
|
|
|
checkout.update(self.checkout_parse('shipping', data))
|
2014-08-01 15:11:20 +00:00
|
|
|
|
|
|
|
if shipping_id is None:
|
|
|
|
if not order:
|
|
|
|
order = request.website.sale_get_order(context=context)
|
|
|
|
if order and order.partner_shipping_id:
|
|
|
|
shipping_id = order.partner_shipping_id.id
|
|
|
|
|
|
|
|
shipping_ids = list(set(shipping_ids) - set([partner.id]))
|
|
|
|
|
|
|
|
if shipping_id == partner.id:
|
|
|
|
shipping_id = 0
|
|
|
|
elif shipping_id > 0 and shipping_id not in shipping_ids:
|
|
|
|
shipping_ids.append(shipping_id)
|
|
|
|
elif shipping_id is None and shipping_ids:
|
|
|
|
shipping_id = shipping_ids[0]
|
|
|
|
|
|
|
|
ctx = dict(context, show_address=1)
|
|
|
|
shippings = []
|
|
|
|
if shipping_ids:
|
|
|
|
shippings = shipping_ids and orm_partner.browse(cr, SUPERUSER_ID, list(shipping_ids), ctx) or []
|
|
|
|
if shipping_id > 0:
|
|
|
|
shipping = orm_partner.browse(cr, SUPERUSER_ID, shipping_id, ctx)
|
|
|
|
checkout.update( self.checkout_parse("shipping", shipping) )
|
|
|
|
|
|
|
|
checkout['shipping_id'] = shipping_id
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-06-18 08:04:40 +00:00
|
|
|
# Default search by user country
|
2014-09-30 15:54:25 +00:00
|
|
|
if not checkout.get('country_id'):
|
|
|
|
country_code = request.session['geoip'].get('country_code')
|
|
|
|
if country_code:
|
|
|
|
country_ids = request.registry.get('res.country').search(cr, uid, [('code', '=', country_code)], context=context)
|
|
|
|
if country_ids:
|
|
|
|
checkout['country_id'] = country_ids[0]
|
2014-06-18 08:04:40 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-11-13 18:43:03 +00:00
|
|
|
'countries': countries,
|
|
|
|
'states': states,
|
2014-02-25 10:09:34 +00:00
|
|
|
'checkout': checkout,
|
2014-08-01 15:11:20 +00:00
|
|
|
'shipping_id': partner.id != shipping_id and shipping_id or 0,
|
|
|
|
'shippings': shippings,
|
2013-11-13 18:43:03 +00:00
|
|
|
'error': {},
|
2014-06-18 08:04:40 +00:00
|
|
|
'has_check_vat': hasattr(registry['res.partner'], 'check_vat')
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2014-08-01 15:11:20 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
return values
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2014-10-07 17:00:45 +00:00
|
|
|
mandatory_billing_fields = ["name", "phone", "email", "street2", "city", "country_id", "zip"]
|
|
|
|
optional_billing_fields = ["street", "state_id", "vat"]
|
2014-02-24 16:20:41 +00:00
|
|
|
mandatory_shipping_fields = ["name", "phone", "street", "city", "country_id", "zip"]
|
|
|
|
optional_shipping_fields = ["state_id"]
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2014-04-10 15:13:30 +00:00
|
|
|
def checkout_parse(self, address_type, data, remove_prefix=False):
|
2014-02-24 16:20:41 +00:00
|
|
|
""" data is a dict OR a partner browse record
|
|
|
|
"""
|
|
|
|
# set mandatory and optional fields
|
2014-01-30 23:39:51 +00:00
|
|
|
assert address_type in ('billing', 'shipping')
|
|
|
|
if address_type == 'billing':
|
2014-02-24 16:20:41 +00:00
|
|
|
all_fields = self.mandatory_billing_fields + self.optional_billing_fields
|
2014-01-30 23:39:51 +00:00
|
|
|
prefix = ''
|
|
|
|
else:
|
2014-02-24 16:20:41 +00:00
|
|
|
all_fields = self.mandatory_shipping_fields + self.optional_shipping_fields
|
2014-01-30 23:39:51 +00:00
|
|
|
prefix = 'shipping_'
|
|
|
|
|
2014-02-24 16:20:41 +00:00
|
|
|
# set data
|
|
|
|
if isinstance(data, dict):
|
2014-04-10 15:37:46 +00:00
|
|
|
query = dict((prefix + field_name, data[prefix + field_name])
|
|
|
|
for field_name in all_fields if data.get(prefix + field_name))
|
2014-02-24 16:20:41 +00:00
|
|
|
else:
|
2014-04-10 15:37:46 +00:00
|
|
|
query = dict((prefix + field_name, getattr(data, field_name))
|
2014-10-07 17:00:45 +00:00
|
|
|
for field_name in all_fields if getattr(data, field_name))
|
2014-02-24 16:20:41 +00:00
|
|
|
if data.parent_id:
|
2014-10-07 17:00:45 +00:00
|
|
|
query[prefix + 'street'] = data.parent_id.name
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-24 16:20:41 +00:00
|
|
|
if query.get(prefix + 'state_id'):
|
|
|
|
query[prefix + 'state_id'] = int(query[prefix + 'state_id'])
|
|
|
|
if query.get(prefix + 'country_id'):
|
|
|
|
query[prefix + 'country_id'] = int(query[prefix + 'country_id'])
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-04-10 15:13:30 +00:00
|
|
|
if not remove_prefix:
|
|
|
|
return query
|
|
|
|
|
|
|
|
return dict((field_name, data[prefix + field_name]) for field_name in all_fields if data.get(prefix + field_name))
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-24 16:20:41 +00:00
|
|
|
def checkout_form_validate(self, data):
|
2014-01-30 23:39:51 +00:00
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
# Validation
|
2014-02-24 16:20:41 +00:00
|
|
|
error = dict()
|
|
|
|
for field_name in self.mandatory_billing_fields:
|
|
|
|
if not data.get(field_name):
|
2013-11-13 18:43:03 +00:00
|
|
|
error[field_name] = 'missing'
|
2014-02-24 16:20:41 +00:00
|
|
|
|
2014-05-27 10:49:16 +00:00
|
|
|
if data.get("vat") and hasattr(registry["res.partner"], "check_vat"):
|
2014-05-27 12:35:07 +00:00
|
|
|
if request.website.company_id.vat_check_vies:
|
|
|
|
# force full VIES online check
|
|
|
|
check_func = registry["res.partner"].vies_vat_check
|
|
|
|
else:
|
|
|
|
# quick and partial off-line checksum validation
|
|
|
|
check_func = registry["res.partner"].simple_vat_check
|
2014-05-27 10:49:16 +00:00
|
|
|
vat_country, vat_number = registry["res.partner"]._split_vat(data.get("vat"))
|
2014-05-27 12:35:07 +00:00
|
|
|
if not check_func(cr, uid, vat_country, vat_number, context=None): # simple_vat_check
|
2014-05-27 10:49:16 +00:00
|
|
|
error["vat"] = 'error'
|
|
|
|
|
2014-08-01 15:11:20 +00:00
|
|
|
if data.get("shipping_id") == -1:
|
2014-02-24 16:20:41 +00:00
|
|
|
for field_name in self.mandatory_shipping_fields:
|
2014-04-10 14:35:24 +00:00
|
|
|
field_name = 'shipping_' + field_name
|
2014-02-24 16:20:41 +00:00
|
|
|
if not data.get(field_name):
|
2013-11-13 18:43:03 +00:00
|
|
|
error[field_name] = 'missing'
|
2013-07-23 13:39:23 +00:00
|
|
|
|
2014-02-24 16:20:41 +00:00
|
|
|
return error
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-04-10 15:13:30 +00:00
|
|
|
def checkout_form_save(self, checkout):
|
2014-02-25 10:09:34 +00:00
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
|
|
|
|
|
|
|
order = request.website.sale_get_order(force_create=1, context=context)
|
|
|
|
|
|
|
|
orm_partner = registry.get('res.partner')
|
|
|
|
orm_user = registry.get('res.users')
|
2014-05-05 12:47:02 +00:00
|
|
|
order_obj = request.registry.get('sale.order')
|
2014-02-25 10:09:34 +00:00
|
|
|
|
2014-04-10 15:13:30 +00:00
|
|
|
billing_info = self.checkout_parse('billing', checkout, True)
|
2013-10-08 15:22:33 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
# set partner_id
|
2014-01-28 15:21:36 +00:00
|
|
|
partner_id = None
|
2014-02-21 17:28:16 +00:00
|
|
|
if request.uid != request.website.user_id.id:
|
2014-01-17 15:46:48 +00:00
|
|
|
partner_id = orm_user.browse(cr, SUPERUSER_ID, uid, context=context).partner_id.id
|
2014-01-28 14:52:46 +00:00
|
|
|
elif order.partner_id:
|
2014-04-28 09:36:09 +00:00
|
|
|
user_ids = request.registry['res.users'].search(cr, SUPERUSER_ID,
|
|
|
|
[("partner_id", "=", order.partner_id.id)], context=dict(context or {}, active_test=False))
|
2014-02-21 17:28:16 +00:00
|
|
|
if not user_ids or request.website.user_id.id not in user_ids:
|
2014-01-28 14:52:46 +00:00
|
|
|
partner_id = order.partner_id.id
|
2014-01-28 15:21:36 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
# save partner informations
|
2014-05-02 16:06:28 +00:00
|
|
|
if partner_id and request.website.partner_id.id != partner_id:
|
2014-01-28 14:52:46 +00:00
|
|
|
orm_partner.write(cr, SUPERUSER_ID, [partner_id], billing_info, context=context)
|
2013-11-13 18:43:03 +00:00
|
|
|
else:
|
2014-06-29 21:31:16 +00:00
|
|
|
# create partner
|
2014-01-28 14:52:46 +00:00
|
|
|
partner_id = orm_partner.create(cr, SUPERUSER_ID, billing_info, context=context)
|
2013-07-23 13:39:23 +00:00
|
|
|
|
2014-04-25 13:20:49 +00:00
|
|
|
# create a new shipping partner
|
2014-08-01 15:11:20 +00:00
|
|
|
if checkout.get('shipping_id') == -1:
|
2014-04-10 15:13:30 +00:00
|
|
|
shipping_info = self.checkout_parse('shipping', checkout, True)
|
2014-02-25 10:09:34 +00:00
|
|
|
shipping_info['type'] = 'delivery'
|
|
|
|
shipping_info['parent_id'] = partner_id
|
2014-08-01 15:11:20 +00:00
|
|
|
checkout['shipping_id'] = orm_partner.create(cr, SUPERUSER_ID, shipping_info, context)
|
2013-07-02 12:06:25 +00:00
|
|
|
|
2013-11-13 18:43:03 +00:00
|
|
|
order_info = {
|
2013-07-23 16:13:19 +00:00
|
|
|
'partner_id': partner_id,
|
2014-09-26 13:09:29 +00:00
|
|
|
'message_follower_ids': [(4, partner_id), (3, request.website.partner_id.id)],
|
2013-07-23 16:13:19 +00:00
|
|
|
'partner_invoice_id': partner_id,
|
2013-07-24 09:42:18 +00:00
|
|
|
}
|
2014-06-29 21:31:16 +00:00
|
|
|
order_info.update(order_obj.onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
|
2014-08-04 14:56:10 +00:00
|
|
|
order_info.update(order_obj.onchange_delivery_id(cr, SUPERUSER_ID, [], order.company_id.id, partner_id, checkout.get('shipping_id'), None, context=context)['value'])
|
2014-06-29 21:31:16 +00:00
|
|
|
|
2014-01-24 11:42:24 +00:00
|
|
|
order_info.pop('user_id')
|
2014-08-01 15:11:20 +00:00
|
|
|
order_info.update(partner_shipping_id=checkout.get('shipping_id') or partner_id)
|
2013-12-03 15:44:22 +00:00
|
|
|
|
2014-05-05 12:47:02 +00:00
|
|
|
order_obj.write(cr, SUPERUSER_ID, [order.id], order_info, context=context)
|
2013-07-23 16:13:19 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/checkout'], type='http', auth="public", website=True)
|
2014-01-30 23:39:51 +00:00
|
|
|
def checkout(self, **post):
|
2014-06-16 15:33:18 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
order = request.website.sale_get_order(force_create=1, context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
redirection = self.checkout_redirection(order)
|
2014-01-30 23:39:51 +00:00
|
|
|
if redirection:
|
|
|
|
return redirection
|
|
|
|
|
|
|
|
values = self.checkout_values()
|
|
|
|
|
|
|
|
return request.website.render("website_sale.checkout", values)
|
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/confirm_order'], type='http', auth="public", website=True)
|
2014-01-30 23:39:51 +00:00
|
|
|
def confirm_order(self, **post):
|
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
order = request.website.sale_get_order(context=context)
|
2014-05-11 11:52:31 +00:00
|
|
|
if not order:
|
|
|
|
return request.redirect("/shop")
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
redirection = self.checkout_redirection(order)
|
2014-01-30 23:39:51 +00:00
|
|
|
if redirection:
|
|
|
|
return redirection
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
values = self.checkout_values(post)
|
2014-04-10 14:35:24 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
values["error"] = self.checkout_form_validate(values["checkout"])
|
|
|
|
if values["error"]:
|
2014-01-30 23:39:51 +00:00
|
|
|
return request.website.render("website_sale.checkout", values)
|
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
self.checkout_form_save(values["checkout"])
|
2014-08-01 15:11:20 +00:00
|
|
|
|
2014-02-25 12:57:36 +00:00
|
|
|
request.session['sale_last_order_id'] = order.id
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-06-29 21:31:16 +00:00
|
|
|
request.website.sale_get_order(update_pricelist=True, context=context)
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
return request.redirect("/shop/payment")
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Payment
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/payment'], type='http', auth="public", website=True)
|
2013-11-21 12:09:02 +00:00
|
|
|
def payment(self, **post):
|
|
|
|
""" Payment step. This page proposes several payment means based on available
|
|
|
|
payment.acquirer. State at this point :
|
|
|
|
|
|
|
|
- a draft sale order with lines; otherwise, clean context / session and
|
|
|
|
back to the shop
|
|
|
|
- no transaction in context / session, or only a draft one, if the customer
|
|
|
|
did go to a payment.acquirer website but closed the tab without
|
|
|
|
paying / canceling
|
|
|
|
"""
|
2013-11-13 14:00:09 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-10-17 11:00:46 +00:00
|
|
|
payment_obj = request.registry.get('payment.acquirer')
|
2014-10-08 13:37:21 +00:00
|
|
|
sale_order_obj = request.registry.get('sale.order')
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2014-02-25 10:09:34 +00:00
|
|
|
order = request.website.sale_get_order(context=context)
|
|
|
|
|
|
|
|
redirection = self.checkout_redirection(order)
|
2014-01-30 23:39:51 +00:00
|
|
|
if redirection:
|
|
|
|
return redirection
|
2013-11-19 16:01:29 +00:00
|
|
|
|
|
|
|
shipping_partner_id = False
|
|
|
|
if order:
|
|
|
|
if order.partner_shipping_id.id:
|
|
|
|
shipping_partner_id = order.partner_shipping_id.id
|
2013-12-24 14:58:45 +00:00
|
|
|
else:
|
|
|
|
shipping_partner_id = order.partner_invoice_id.id
|
2013-11-15 13:34:14 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-12-24 14:58:45 +00:00
|
|
|
'order': request.registry['sale.order'].browse(cr, SUPERUSER_ID, order.id, context=context)
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2014-10-08 13:37:21 +00:00
|
|
|
values['errors'] = sale_order_obj._get_errors(cr, uid, order, context=context)
|
|
|
|
values.update(sale_order_obj._get_website_data(cr, uid, order, context))
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2013-11-13 14:00:09 +00:00
|
|
|
# fetch all registered payment means
|
2014-02-25 12:57:36 +00:00
|
|
|
# if tx:
|
|
|
|
# acquirer_ids = [tx.acquirer_id.id]
|
|
|
|
# else:
|
2014-10-08 13:37:21 +00:00
|
|
|
if not values['errors']:
|
|
|
|
acquirer_ids = payment_obj.search(cr, SUPERUSER_ID, [('website_published', '=', True), ('company_id', '=', order.company_id.id)], context=context)
|
|
|
|
values['acquirers'] = list(payment_obj.browse(cr, uid, acquirer_ids, context=context))
|
|
|
|
render_ctx = dict(context, submit_class='btn btn-primary', submit_txt=_('Pay Now'))
|
|
|
|
for acquirer in values['acquirers']:
|
|
|
|
acquirer.button = payment_obj.render(
|
|
|
|
cr, SUPERUSER_ID, acquirer.id,
|
|
|
|
order.name,
|
|
|
|
order.amount_total,
|
|
|
|
order.pricelist_id.currency_id.id,
|
|
|
|
partner_id=shipping_partner_id,
|
|
|
|
tx_values={
|
|
|
|
'return_url': '/shop/payment/validate',
|
|
|
|
},
|
|
|
|
context=render_ctx)
|
2013-08-08 14:23:41 +00:00
|
|
|
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_sale.payment", values)
|
2013-08-08 14:23:41 +00:00
|
|
|
|
2014-05-23 07:53:12 +00:00
|
|
|
@http.route(['/shop/payment/transaction/<int:acquirer_id>'], type='json', auth="public", website=True)
|
|
|
|
def payment_transaction(self, acquirer_id):
|
|
|
|
""" Json method that creates a payment.transaction, used to create a
|
|
|
|
transaction when the user clicks on 'pay now' button. After having
|
|
|
|
created the transaction, the event continues and the user is redirected
|
|
|
|
to the acquirer website.
|
2013-11-15 13:34:14 +00:00
|
|
|
|
|
|
|
:param int acquirer_id: id of a payment.acquirer record. If not set the
|
|
|
|
user is redirected to the checkout page
|
|
|
|
"""
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
transaction_obj = request.registry.get('payment.transaction')
|
2014-02-25 10:09:34 +00:00
|
|
|
order = request.website.sale_get_order(context=context)
|
2013-11-15 13:34:14 +00:00
|
|
|
|
2013-11-20 14:03:21 +00:00
|
|
|
if not order or not order.order_line or acquirer_id is None:
|
2014-04-10 14:35:24 +00:00
|
|
|
return request.redirect("/shop/checkout")
|
2013-11-15 13:34:14 +00:00
|
|
|
|
2014-05-05 12:47:02 +00:00
|
|
|
assert order.partner_id.id != request.website.partner_id.id
|
|
|
|
|
2013-11-15 13:34:14 +00:00
|
|
|
# find an already existing transaction
|
2014-02-25 12:57:36 +00:00
|
|
|
tx = request.website.sale_get_transaction()
|
2014-05-23 07:53:12 +00:00
|
|
|
if tx:
|
|
|
|
if tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ?
|
|
|
|
tx.write({
|
|
|
|
'acquirer_id': acquirer_id,
|
|
|
|
})
|
|
|
|
tx_id = tx.id
|
|
|
|
else:
|
2013-12-24 14:58:45 +00:00
|
|
|
tx_id = transaction_obj.create(cr, SUPERUSER_ID, {
|
2013-11-15 13:34:14 +00:00
|
|
|
'acquirer_id': acquirer_id,
|
|
|
|
'type': 'form',
|
|
|
|
'amount': order.amount_total,
|
|
|
|
'currency_id': order.pricelist_id.currency_id.id,
|
|
|
|
'partner_id': order.partner_id.id,
|
2014-05-05 12:47:02 +00:00
|
|
|
'partner_country_id': order.partner_id.country_id.id,
|
2013-11-15 13:34:14 +00:00
|
|
|
'reference': order.name,
|
2013-11-21 12:09:02 +00:00
|
|
|
'sale_order_id': order.id,
|
2013-11-15 13:34:14 +00:00
|
|
|
}, context=context)
|
2014-02-25 12:57:36 +00:00
|
|
|
request.session['sale_transaction_id'] = tx_id
|
2013-11-15 13:34:14 +00:00
|
|
|
|
2014-05-12 15:52:10 +00:00
|
|
|
# update quotation
|
2014-09-03 11:21:23 +00:00
|
|
|
request.registry['sale.order'].write(
|
2014-05-12 15:52:10 +00:00
|
|
|
cr, SUPERUSER_ID, [order.id], {
|
|
|
|
'payment_acquirer_id': acquirer_id,
|
|
|
|
'payment_tx_id': request.session['sale_transaction_id']
|
|
|
|
}, context=context)
|
|
|
|
|
2014-05-23 07:53:12 +00:00
|
|
|
return tx_id
|
2013-11-15 13:34:14 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route('/shop/payment/get_status/<int:sale_order_id>', type='json', auth="public", website=True)
|
2014-01-07 13:59:33 +00:00
|
|
|
def payment_get_status(self, sale_order_id, **post):
|
2013-11-19 16:01:29 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2014-01-07 13:59:33 +00:00
|
|
|
|
|
|
|
order = request.registry['sale.order'].browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
2014-02-25 12:57:36 +00:00
|
|
|
assert order.id == request.session.get('sale_last_order_id')
|
2014-01-07 13:59:33 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if not order:
|
|
|
|
return {
|
|
|
|
'state': 'error',
|
2014-07-23 10:00:09 +00:00
|
|
|
'message': '<p>%s</p>' % _('There seems to be an error with your request.'),
|
2013-11-21 12:09:02 +00:00
|
|
|
}
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
tx_ids = request.registry['payment.transaction'].search(
|
2014-09-17 10:28:21 +00:00
|
|
|
cr, SUPERUSER_ID, [
|
2013-11-21 12:09:02 +00:00
|
|
|
'|', ('sale_order_id', '=', order.id), ('reference', '=', order.name)
|
|
|
|
], context=context)
|
2014-01-28 16:38:15 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if not tx_ids:
|
2014-01-28 16:38:15 +00:00
|
|
|
if order.amount_total:
|
|
|
|
return {
|
|
|
|
'state': 'error',
|
2014-07-23 10:00:09 +00:00
|
|
|
'message': '<p>%s</p>' % _('There seems to be an error with your request.'),
|
2014-01-28 16:38:15 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
state = 'done'
|
|
|
|
message = ""
|
|
|
|
validation = None
|
2014-01-24 14:43:55 +00:00
|
|
|
else:
|
2014-09-17 10:28:21 +00:00
|
|
|
tx = request.registry['payment.transaction'].browse(cr, SUPERUSER_ID, tx_ids[0], context=context)
|
2014-01-28 16:38:15 +00:00
|
|
|
state = tx.state
|
|
|
|
if state == 'done':
|
2014-07-23 10:00:09 +00:00
|
|
|
message = '<p>%s</p>' % _('Your payment has been received.')
|
2014-01-28 16:38:15 +00:00
|
|
|
elif state == 'cancel':
|
2014-07-23 10:00:09 +00:00
|
|
|
message = '<p>%s</p>' % _('The payment seems to have been canceled.')
|
2014-01-28 16:38:15 +00:00
|
|
|
elif state == 'pending' and tx.acquirer_id.validation == 'manual':
|
2014-07-23 10:00:09 +00:00
|
|
|
message = '<p>%s</p>' % _('Your transaction is waiting confirmation.')
|
2014-05-05 09:34:23 +00:00
|
|
|
if tx.acquirer_id.post_msg:
|
|
|
|
message += tx.acquirer_id.post_msg
|
2014-01-28 16:38:15 +00:00
|
|
|
else:
|
2014-07-23 10:00:09 +00:00
|
|
|
message = '<p>%s</p>' % _('Your transaction is waiting confirmation.')
|
2014-01-28 16:38:15 +00:00
|
|
|
validation = tx.acquirer_id.validation
|
2014-01-24 14:43:55 +00:00
|
|
|
|
2013-11-19 16:01:29 +00:00
|
|
|
return {
|
2014-01-24 14:43:55 +00:00
|
|
|
'state': state,
|
|
|
|
'message': message,
|
2014-01-28 16:38:15 +00:00
|
|
|
'validation': validation
|
2013-11-19 16:01:29 +00:00
|
|
|
}
|
2013-10-17 11:00:46 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route('/shop/payment/validate', type='http', auth="public", website=True)
|
2013-11-21 12:09:02 +00:00
|
|
|
def payment_validate(self, transaction_id=None, sale_order_id=None, **post):
|
|
|
|
""" Method that should be called by the server when receiving an update
|
|
|
|
for a transaction. State at this point :
|
|
|
|
|
|
|
|
- UDPATE ME
|
|
|
|
"""
|
2013-11-20 14:03:21 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
email_act = None
|
|
|
|
sale_order_obj = request.registry['sale.order']
|
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if transaction_id is None:
|
2014-02-25 12:57:36 +00:00
|
|
|
tx = request.website.sale_get_transaction()
|
2013-11-21 12:09:02 +00:00
|
|
|
else:
|
|
|
|
tx = request.registry['payment.transaction'].browse(cr, uid, transaction_id, context=context)
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if sale_order_id is None:
|
2014-02-25 10:09:34 +00:00
|
|
|
order = request.website.sale_get_order(context=context)
|
2013-11-21 12:09:02 +00:00
|
|
|
else:
|
2014-01-07 13:59:33 +00:00
|
|
|
order = request.registry['sale.order'].browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
2014-02-25 12:57:36 +00:00
|
|
|
assert order.id == request.session.get('sale_last_order_id')
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2014-09-25 14:24:59 +00:00
|
|
|
if not order or (order.amount_total and not tx):
|
2014-04-10 14:35:24 +00:00
|
|
|
return request.redirect('/shop')
|
2014-01-28 16:38:15 +00:00
|
|
|
|
2014-09-25 14:24:59 +00:00
|
|
|
if (not order.amount_total and not tx) or tx.state in ['pending', 'done']:
|
2014-09-26 11:03:43 +00:00
|
|
|
if (not order.amount_total and not tx):
|
|
|
|
# Orders are confirmed by payment transactions, but there is none for free orders,
|
|
|
|
# (e.g. free events), so confirm immediately
|
|
|
|
order.action_button_confirm()
|
2013-11-20 14:03:21 +00:00
|
|
|
# send by email
|
|
|
|
email_act = sale_order_obj.action_quotation_send(cr, SUPERUSER_ID, [order.id], context=request.context)
|
2014-09-25 14:24:59 +00:00
|
|
|
elif tx and tx.state == 'cancel':
|
2013-11-20 14:03:21 +00:00
|
|
|
# cancel the quotation
|
|
|
|
sale_order_obj.action_cancel(cr, SUPERUSER_ID, [order.id], context=request.context)
|
|
|
|
|
2014-05-13 10:32:53 +00:00
|
|
|
# send the email
|
|
|
|
if email_act and email_act.get('context'):
|
|
|
|
composer_values = {}
|
|
|
|
email_ctx = email_act['context']
|
|
|
|
public_id = request.website.user_id.id
|
|
|
|
if uid == public_id:
|
|
|
|
composer_values['email_from'] = request.website.user_id.company_id.email
|
|
|
|
composer_id = request.registry['mail.compose.message'].create(cr, SUPERUSER_ID, composer_values, context=email_ctx)
|
|
|
|
request.registry['mail.compose.message'].send_mail(cr, SUPERUSER_ID, [composer_id], context=email_ctx)
|
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
# clean context and session, then redirect to the confirmation page
|
2014-02-25 12:57:36 +00:00
|
|
|
request.website.sale_reset(context=context)
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2014-04-10 14:35:24 +00:00
|
|
|
return request.redirect('/shop/confirmation')
|
2013-10-17 11:00:46 +00:00
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/confirmation'], type='http', auth="public", website=True)
|
2014-02-25 12:57:36 +00:00
|
|
|
def payment_confirmation(self, **post):
|
2013-11-21 12:09:02 +00:00
|
|
|
""" End of checkout process controller. Confirmation is basically seing
|
|
|
|
the status of a sale.order. State at this point :
|
2013-10-17 11:00:46 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
- should not have any context / session info: clean them
|
|
|
|
- take a sale.order id, because we request a sale.order and are not
|
|
|
|
session dependant anymore
|
|
|
|
"""
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-11-19 16:01:29 +00:00
|
|
|
|
2014-02-25 12:57:36 +00:00
|
|
|
sale_order_id = request.session.get('sale_last_order_id')
|
|
|
|
if sale_order_id:
|
|
|
|
order = request.registry['sale.order'].browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
|
|
|
else:
|
2014-04-10 14:35:24 +00:00
|
|
|
return request.redirect('/shop')
|
2014-01-22 12:48:09 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.website.render("website_sale.confirmation", {'order': order})
|
2013-08-23 09:40:17 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Edit
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2014-05-13 09:35:45 +00:00
|
|
|
@http.route(['/shop/add_product'], type='http', auth="user", methods=['POST'], website=True)
|
2014-02-25 12:57:36 +00:00
|
|
|
def add_product(self, name=None, category=0, **post):
|
2014-01-30 23:39:51 +00:00
|
|
|
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
|
2014-02-25 12:57:36 +00:00
|
|
|
if not name:
|
|
|
|
name = _("New Product")
|
|
|
|
product_obj = request.registry.get('product.product')
|
2014-05-27 07:42:52 +00:00
|
|
|
product_id = product_obj.create(cr, uid, { 'name': name, 'public_categ_ids': category }, context=context)
|
2014-02-25 12:57:36 +00:00
|
|
|
product = product_obj.browse(cr, uid, product_id, context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-05-05 09:57:24 +00:00
|
|
|
return request.redirect("/shop/product/%s?enable_editor=1" % slug(product.product_tmpl_id))
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
@http.route(['/shop/change_styles'], type='json', auth="public")
|
2013-09-26 13:09:54 +00:00
|
|
|
def change_styles(self, id, style_id):
|
2013-09-27 10:31:48 +00:00
|
|
|
product_obj = request.registry.get('product.template')
|
2013-10-02 07:12:39 +00:00
|
|
|
product = product_obj.browse(request.cr, request.uid, id, context=request.context)
|
2013-09-26 13:09:54 +00:00
|
|
|
|
|
|
|
remove = []
|
|
|
|
active = False
|
|
|
|
for style in product.website_style_ids:
|
|
|
|
if style.id == style_id:
|
|
|
|
remove.append(style.id)
|
|
|
|
active = True
|
|
|
|
break
|
|
|
|
|
2014-01-30 22:58:53 +00:00
|
|
|
style = request.registry.get('product.style').browse(request.cr, request.uid, style_id, context=request.context)
|
2013-09-26 13:09:54 +00:00
|
|
|
|
|
|
|
if remove:
|
|
|
|
product.write({'website_style_ids': [(3, rid) for rid in remove]})
|
|
|
|
if not active:
|
|
|
|
product.write({'website_style_ids': [(4, style.id)]})
|
|
|
|
|
|
|
|
return not active
|
|
|
|
|
2014-05-30 13:20:18 +00:00
|
|
|
@http.route(['/shop/change_sequence'], type='json', auth="public")
|
|
|
|
def change_sequence(self, id, sequence):
|
|
|
|
product_obj = request.registry.get('product.template')
|
|
|
|
if sequence == "top":
|
|
|
|
product_obj.set_sequence_top(request.cr, request.uid, [id], context=request.context)
|
|
|
|
elif sequence == "bottom":
|
|
|
|
product_obj.set_sequence_bottom(request.cr, request.uid, [id], context=request.context)
|
|
|
|
elif sequence == "up":
|
|
|
|
product_obj.set_sequence_up(request.cr, request.uid, [id], context=request.context)
|
|
|
|
elif sequence == "down":
|
|
|
|
product_obj.set_sequence_down(request.cr, request.uid, [id], context=request.context)
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
@http.route(['/shop/change_size'], type='json', auth="public")
|
2013-09-27 10:31:48 +00:00
|
|
|
def change_size(self, id, x, y):
|
|
|
|
product_obj = request.registry.get('product.template')
|
2013-10-02 07:12:39 +00:00
|
|
|
product = product_obj.browse(request.cr, request.uid, id, context=request.context)
|
2013-09-27 10:31:48 +00:00
|
|
|
return product.write({'website_size_x': x, 'website_size_y': y})
|
2013-09-26 11:18:54 +00:00
|
|
|
|
2014-07-24 09:07:46 +00:00
|
|
|
def order_lines_2_google_api(self, order_lines):
|
|
|
|
""" Transforms a list of order lines into a dict for google analytics """
|
|
|
|
ret = []
|
|
|
|
for line in order_lines:
|
|
|
|
ret.append({
|
|
|
|
'id': line.order_id and line.order_id.id,
|
|
|
|
'name': line.product_id.categ_id and line.product_id.categ_id.name or '-',
|
|
|
|
'sku': line.product_id.id,
|
|
|
|
'quantity': line.product_uom_qty,
|
|
|
|
'price': line.price_unit,
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
|
2014-07-09 15:45:40 +00:00
|
|
|
@http.route(['/shop/tracking_last_order'], type='json', auth="public")
|
|
|
|
def tracking_cart(self, **post):
|
2014-07-24 09:07:46 +00:00
|
|
|
""" return data about order in JSON needed for google analytics"""
|
2014-07-09 15:45:40 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
ret = {}
|
|
|
|
sale_order_id = request.session.get('sale_last_order_id')
|
|
|
|
if sale_order_id:
|
|
|
|
order = request.registry['sale.order'].browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
|
|
|
ret['transaction'] = {
|
|
|
|
'id': sale_order_id,
|
|
|
|
'affiliation': order.company_id.name,
|
|
|
|
'revenue': order.amount_total,
|
|
|
|
'currency': order.currency_id.name
|
|
|
|
}
|
2014-07-24 09:07:46 +00:00
|
|
|
ret['lines'] = self.order_lines_2_google_api(order.order_line)
|
2014-07-09 15:45:40 +00:00
|
|
|
return ret
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2013-06-28 16:28:04 +00:00
|
|
|
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|