2013-06-28 16:28:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-10-30 13:30:19 +00:00
|
|
|
import random
|
|
|
|
import simplejson
|
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
|
2013-06-28 16:28:04 +00:00
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2014-02-06 15:47:11 +00:00
|
|
|
from openerp.tools.translate import _
|
2014-02-12 11:55:16 +00:00
|
|
|
from openerp.addons.website.models.website import slug
|
2013-06-28 16:28:04 +00:00
|
|
|
|
2013-12-01 00:23:55 +00:00
|
|
|
PPG = 20 # Products Per Page
|
|
|
|
PPR = 4 # Products Per Row
|
2013-11-13 14:00:09 +00:00
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2013-11-26 08:51:22 +00:00
|
|
|
class CheckoutInfo(object):
|
2013-11-13 18:43:03 +00:00
|
|
|
mandatory_billing_fields = ["name", "phone", "email", "street", "city", "country_id", "zip"]
|
|
|
|
optional_billing_fields = ["company", "state_id"]
|
|
|
|
string_billing_fields = ["name", "phone", "email", "street", "city", "zip"]
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2013-11-13 18:43:03 +00:00
|
|
|
mandatory_shipping_fields = ["shipping_name", "shipping_phone", "shipping_street", "shipping_city", "shipping_country_id", "shipping_zip"]
|
|
|
|
optional_shipping_field = ["shipping_state_id"]
|
2013-12-18 13:21:50 +00:00
|
|
|
string_shipping_fields = ["shipping_name", "shipping_phone", "shipping_street", "shipping_city", "shipping_zip"]
|
2013-11-13 18:43:03 +00:00
|
|
|
|
|
|
|
def mandatory_fields(self):
|
|
|
|
return self.mandatory_billing_fields + self.mandatory_shipping_fields
|
|
|
|
|
|
|
|
def optional_fields(self):
|
|
|
|
return self.optional_billing_fields + self.optional_shipping_field
|
|
|
|
|
|
|
|
def all_fields(self):
|
|
|
|
return self.mandatory_fields() + self.optional_fields()
|
|
|
|
|
|
|
|
def empty(self):
|
2013-11-26 08:51:22 +00:00
|
|
|
return dict.fromkeys(self.all_fields(), '')
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
def from_partner(self, partner, address_type='billing'):
|
|
|
|
assert address_type in ('billing', 'shipping')
|
|
|
|
if address_type == 'billing':
|
|
|
|
prefix = ''
|
|
|
|
else:
|
|
|
|
prefix = 'shipping_'
|
|
|
|
result = dict((prefix + field_name, getattr(partner, field_name)) for field_name in self.string_billing_fields if getattr(partner, field_name))
|
|
|
|
result[prefix + 'state_id'] = partner.state_id and partner.state_id.id or ''
|
|
|
|
result[prefix + 'country_id'] = partner.country_id and partner.country_id.id or ''
|
2014-04-10 09:58:14 +00:00
|
|
|
result[prefix + 'company'] = partner.commercial_partner_id and partner.commercial_partner_id.is_company and partner.commercial_partner_id.name or ''
|
2013-11-13 18:43:03 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def from_post(self, post):
|
|
|
|
return dict((field_name, post[field_name]) for field_name in self.all_fields() if post[field_name])
|
|
|
|
|
2013-11-15 13:36:35 +00:00
|
|
|
|
2013-12-01 00:23:55 +00:00
|
|
|
#
|
|
|
|
# Compute grid of products according to their sizes
|
|
|
|
#
|
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:
|
|
|
|
x = p.website_size_x
|
|
|
|
y = p.website_size_y
|
|
|
|
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):
|
2014-03-07 16:52:39 +00:00
|
|
|
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,
|
|
|
|
'class': " ".join(map(lambda x: x.html_class, p.website_style_ids))
|
|
|
|
}
|
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
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
|
2013-06-28 16:28:04 +00:00
|
|
|
class Ecommerce(http.Controller):
|
|
|
|
|
2013-12-12 11:47:31 +00:00
|
|
|
_order = 'website_published desc, website_sequence desc'
|
2013-09-26 11:18:54 +00:00
|
|
|
|
2014-01-30 22:17:10 +00:00
|
|
|
def get_attribute_ids(self):
|
|
|
|
attributes_obj = request.registry['product.attribute']
|
|
|
|
attributes_ids = attributes_obj.search(request.cr, request.uid, [], context=request.context)
|
|
|
|
return attributes_obj.browse(request.cr, request.uid, attributes_ids, context=request.context)
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2013-11-27 11:16:58 +00:00
|
|
|
def get_pricelist(self):
|
2013-12-18 13:21:50 +00:00
|
|
|
""" Shortcut to get the pricelist from the website model """
|
|
|
|
return request.registry['website'].ecommerce_get_pricelist_id(request.cr, request.uid, None, context=request.context)
|
|
|
|
|
|
|
|
def get_order(self):
|
|
|
|
""" Shortcut to get the current ecommerce quotation from the website model """
|
|
|
|
return request.registry['website'].ecommerce_get_current_order(request.cr, request.uid, context=request.context)
|
2013-11-27 11:16:58 +00:00
|
|
|
|
2013-09-26 07:46:44 +00:00
|
|
|
def get_products(self, product_ids):
|
|
|
|
product_obj = request.registry.get('product.template')
|
2013-09-27 14:46:36 +00:00
|
|
|
request.context['pricelist'] = self.get_pricelist()
|
2013-09-26 11:18:54 +00:00
|
|
|
# search for checking of access rules and keep order
|
|
|
|
product_ids = [id for id in product_ids if id in product_obj.search(request.cr, request.uid, [("id", 'in', product_ids)], context=request.context)]
|
2013-10-02 08:37:50 +00:00
|
|
|
return product_obj.browse(request.cr, request.uid, product_ids, context=request.context)
|
2013-09-26 07:46:44 +00:00
|
|
|
|
2014-01-30 22:17:10 +00:00
|
|
|
def has_search_filter(self, attribute_id, value_id=None):
|
2013-12-05 16:22:40 +00:00
|
|
|
if request.httprequest.args.get('filters'):
|
|
|
|
filters = simplejson.loads(request.httprequest.args['filters'])
|
2013-10-15 08:27:26 +00:00
|
|
|
else:
|
2013-12-05 16:22:40 +00:00
|
|
|
filters = []
|
|
|
|
for key_val in filters:
|
2014-01-30 22:17:10 +00:00
|
|
|
if key_val[0] == attribute_id and (not value_id or value_id in key_val[1:]):
|
2013-10-15 08:27:26 +00:00
|
|
|
return key_val
|
|
|
|
return False
|
|
|
|
|
2014-02-12 11:55:16 +00:00
|
|
|
@http.route(['/shop/filters/'], type='http', auth="public", methods=['POST'], website=True, multilang=True)
|
|
|
|
def filters(self, category=None, **post):
|
2013-10-15 08:27:26 +00:00
|
|
|
index = []
|
2013-12-05 16:22:40 +00:00
|
|
|
filters = []
|
2013-10-15 08:27:26 +00:00
|
|
|
for key, val in post.items():
|
|
|
|
cat = key.split("-")
|
|
|
|
if len(cat) < 3 or cat[2] in ('max','minmem','maxmem'):
|
|
|
|
continue
|
|
|
|
cat_id = int(cat[1])
|
|
|
|
if cat[2] == 'min':
|
|
|
|
minmem = float(post.pop("att-%s-minmem" % cat[1]))
|
|
|
|
maxmem = float(post.pop("att-%s-maxmem" % cat[1]))
|
|
|
|
_max = int(post.pop("att-%s-max" % cat[1]))
|
|
|
|
_min = int(val)
|
|
|
|
if (minmem != _min or maxmem != _max) and cat_id not in index:
|
2013-12-05 16:22:40 +00:00
|
|
|
filters.append([cat_id , [_min, _max] ])
|
2013-10-15 08:27:26 +00:00
|
|
|
index.append(cat_id)
|
|
|
|
elif cat_id not in index:
|
2013-12-05 16:22:40 +00:00
|
|
|
filters.append([ cat_id, int(cat[2]) ])
|
2013-10-15 08:27:26 +00:00
|
|
|
index.append(cat_id)
|
|
|
|
else:
|
2013-11-05 15:14:17 +00:00
|
|
|
cat[2] = int(cat[2])
|
2013-12-05 16:22:40 +00:00
|
|
|
if cat[2] not in filters[index.index(cat_id)][1:]:
|
|
|
|
filters[index.index(cat_id)].append( cat[2] )
|
2013-10-15 08:27:26 +00:00
|
|
|
post.pop(key)
|
|
|
|
|
2014-02-12 11:55:16 +00:00
|
|
|
url = "/shop/"
|
|
|
|
if category:
|
|
|
|
category_obj = request.registry.get('product.public.category')
|
|
|
|
url = "%scategory/%s/" % (url, slug(category_obj.browse(request.cr, request.uid, int(category), context=request.context)))
|
|
|
|
if filters:
|
|
|
|
url = "%s?filters=%s" % (url, simplejson.dumps(filters))
|
|
|
|
if post.get("search"):
|
|
|
|
url = "%s%ssearch=%s" % (url, filters and "&" or "?", post.get("search"))
|
|
|
|
|
|
|
|
return request.redirect(url)
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2014-02-27 16:05:19 +00:00
|
|
|
def attributes_to_ids(self, cr, uid, attributes):
|
|
|
|
req = """
|
|
|
|
SELECT product_tmpl_id as id, count(*) as nb_match
|
|
|
|
FROM product_attribute_line
|
|
|
|
WHERE 1!=1
|
|
|
|
"""
|
|
|
|
nb = 0
|
2014-01-30 22:17:10 +00:00
|
|
|
for key_val in attributes:
|
2014-02-27 16:05:19 +00:00
|
|
|
attribute_id = key_val[0]
|
2013-10-15 08:27:26 +00:00
|
|
|
if isinstance(key_val[1], list):
|
2014-02-27 16:05:19 +00:00
|
|
|
req += " OR ( attribute_id = %s AND value >= %s AND value <= %s)" % \
|
|
|
|
(attribute_id, key_val[1][0], key_val[1][1])
|
|
|
|
nb += 1
|
2013-10-15 08:27:26 +00:00
|
|
|
else:
|
2014-02-27 16:05:19 +00:00
|
|
|
for value_id in key_val[1:]:
|
|
|
|
req += " OR ( attribute_id = %s AND value_id = %s)" % \
|
|
|
|
(attribute_id, value_id)
|
|
|
|
nb += 1
|
|
|
|
|
|
|
|
req += " GROUP BY product_tmpl_id"
|
|
|
|
cr.execute(req)
|
|
|
|
result = cr.fetchall()
|
|
|
|
return [id for id, nb_match in result if nb_match >= nb]
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/pricelist'], type='http', auth="public", website=True, multilang=True)
|
2014-01-22 12:48:09 +00:00
|
|
|
def shop_promo(self, promo=None, **post):
|
|
|
|
request.registry['website']._ecommerce_change_pricelist(request.cr, request.uid, code=promo, context=request.context)
|
|
|
|
return request.redirect("/shop/mycart/")
|
2013-12-01 00:23:55 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route([
|
2013-11-21 13:05:29 +00:00
|
|
|
'/shop/',
|
|
|
|
'/shop/page/<int:page>/',
|
2014-01-14 14:29:40 +00:00
|
|
|
'/shop/category/<model("product.public.category"):category>/',
|
|
|
|
'/shop/category/<model("product.public.category"):category>/page/<int:page>/'
|
2014-01-20 15:37:33 +00:00
|
|
|
], type='http', auth="public", website=True, multilang=True)
|
2014-01-14 14:29:40 +00:00
|
|
|
def shop(self, category=None, page=0, filters='', search='', **post):
|
2013-12-01 00:23:55 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-08-20 14:12:03 +00:00
|
|
|
product_obj = request.registry.get('product.template')
|
2014-03-19 14:11:30 +00:00
|
|
|
base_domain = request.registry.get('website').ecommerce_get_product_domain()
|
|
|
|
domain = list(base_domain)
|
2013-11-21 14:16:47 +00:00
|
|
|
if search:
|
2013-12-01 00:23:55 +00:00
|
|
|
domain += ['|',
|
2014-01-29 16:35:57 +00:00
|
|
|
('name', 'ilike', search),
|
|
|
|
('description', 'ilike', search)]
|
2013-10-15 08:33:31 +00:00
|
|
|
if category:
|
2014-02-12 11:55:16 +00:00
|
|
|
domain.append(('product_variant_ids.public_categ_id', 'child_of', int(category)))
|
|
|
|
if isinstance(category, (int,str,unicode)):
|
|
|
|
category = request.registry.get('product.public.category').browse(cr, uid, int(category), context=context)
|
2013-12-05 16:22:40 +00:00
|
|
|
if filters:
|
|
|
|
filters = simplejson.loads(filters)
|
|
|
|
if filters:
|
2014-02-27 16:05:19 +00:00
|
|
|
ids = self.attributes_to_ids(cr, uid, filters)
|
2013-11-21 14:16:47 +00:00
|
|
|
domain.append(('id', 'in', ids or [0]))
|
2013-10-15 08:27:26 +00:00
|
|
|
|
2014-02-12 11:55:16 +00:00
|
|
|
url = "/shop/"
|
2013-12-01 00:23:55 +00:00
|
|
|
product_count = product_obj.search_count(cr, uid, domain, context=context)
|
2014-02-12 11:55:16 +00:00
|
|
|
if search:
|
|
|
|
post["search"] = search
|
|
|
|
if filters:
|
|
|
|
post["filters"] = filters
|
|
|
|
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)
|
2013-08-06 09:23:44 +00:00
|
|
|
|
2013-09-05 15:18:03 +00:00
|
|
|
request.context['pricelist'] = self.get_pricelist()
|
2013-09-26 11:18:54 +00:00
|
|
|
|
2013-12-01 00:23:55 +00:00
|
|
|
pids = product_obj.search(cr, uid, domain, limit=PPG+10, offset=pager['offset'], order=self._order, context=context)
|
|
|
|
products = product_obj.browse(cr, uid, pids, context=context)
|
2013-08-19 13:19:29 +00:00
|
|
|
|
2013-09-27 10:31:48 +00:00
|
|
|
styles = []
|
2013-11-30 15:46:59 +00:00
|
|
|
try:
|
2014-01-30 22:58:53 +00:00
|
|
|
style_obj = request.registry.get('product.style')
|
2013-11-21 14:16:47 +00:00
|
|
|
style_ids = style_obj.search(request.cr, request.uid, [], context=request.context)
|
2013-09-27 10:31:48 +00:00
|
|
|
styles = style_obj.browse(request.cr, request.uid, style_ids, context=request.context)
|
2013-11-30 15:46:59 +00:00
|
|
|
except:
|
|
|
|
pass
|
2013-09-27 10:31:48 +00:00
|
|
|
|
2013-12-01 17:25:41 +00:00
|
|
|
category_obj = request.registry.get('product.public.category')
|
2014-03-19 14:11:30 +00:00
|
|
|
category_ids = [product['public_categ_id'][0] for product in product_obj.read_group(cr, uid, base_domain, ['public_categ_id'], ['public_categ_id'], context=context) if product['public_categ_id']]
|
2013-12-01 17:25:41 +00:00
|
|
|
categories = category_obj.browse(cr, uid, category_ids, context=context)
|
2014-03-19 14:11:30 +00:00
|
|
|
all_categories = set(categories)
|
|
|
|
for cat in categories:
|
|
|
|
parent = cat.parent_id
|
|
|
|
while parent:
|
|
|
|
all_categories.add(parent)
|
|
|
|
parent = parent.parent_id
|
|
|
|
categories = list(all_categories)
|
2014-04-03 11:25:11 +00:00
|
|
|
categories.sort(key=lambda x: x.sequence)
|
2013-12-01 17:25:41 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
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,
|
|
|
|
'range': range,
|
2013-11-06 17:57:43 +00:00
|
|
|
'search': {
|
2013-11-21 14:16:47 +00:00
|
|
|
'search': search,
|
2014-02-12 14:17:13 +00:00
|
|
|
'category': category and int(category),
|
2013-12-05 16:22:40 +00:00
|
|
|
'filters': filters,
|
2013-11-06 17:57:43 +00:00
|
|
|
},
|
2013-08-12 13:43:12 +00:00
|
|
|
'pager': pager,
|
2013-09-27 10:31:48 +00:00
|
|
|
'styles': styles,
|
2014-02-12 11:55:16 +00:00
|
|
|
'category': category,
|
2014-03-19 14:11:30 +00:00
|
|
|
'categories': filter(lambda x: not x.parent_id, categories),
|
|
|
|
'all_categories': categories,
|
2013-12-01 17:25:41 +00:00
|
|
|
'Ecommerce': self, # TODO fp: Should be removed
|
|
|
|
'style_in_product': lambda style, product: style.id in [s.id for s in product.website_style_ids],
|
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-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/product/<model("product.template"):product>/'], type='http', auth="public", website=True, multilang=True)
|
2013-12-05 16:22:40 +00:00
|
|
|
def product(self, product, search='', category='', filters='', **kwargs):
|
2013-11-08 16:49:17 +00:00
|
|
|
if category:
|
2014-02-12 11:55:16 +00:00
|
|
|
category_obj = request.registry.get('product.public.category')
|
2013-11-08 16:49:17 +00:00
|
|
|
category = category_obj.browse(request.cr, request.uid, int(category), context=request.context)
|
2013-09-22 19:33:56 +00:00
|
|
|
|
2013-10-02 07:12:39 +00:00
|
|
|
request.context['pricelist'] = self.get_pricelist()
|
2013-09-26 13:09:54 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-10-15 08:27:26 +00:00
|
|
|
'Ecommerce': self,
|
2013-10-10 13:29:07 +00:00
|
|
|
'main_object': product,
|
2013-08-19 13:19:29 +00:00
|
|
|
'product': product,
|
2014-02-12 11:55:16 +00:00
|
|
|
'category': category,
|
2013-11-06 17:57:43 +00:00
|
|
|
'search': {
|
2013-11-08 16:49:17 +00:00
|
|
|
'search': search,
|
2014-02-12 14:17:13 +00:00
|
|
|
'category': category and int(category),
|
2013-12-05 16:22:40 +00:00
|
|
|
'filters': filters,
|
2013-11-06 17:57:43 +00:00
|
|
|
}
|
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-03-07 10:30:38 +00:00
|
|
|
@http.route(['/shop/product/<int:product_template_id>/comment'], 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-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/add_product/'], type='http', auth="user", methods=['POST'], website=True, multilang=True)
|
2014-02-06 15:47:11 +00:00
|
|
|
def add_product(self, name=None, category=0, **post):
|
|
|
|
if not name:
|
|
|
|
name = _("New Product")
|
2013-10-30 13:30:19 +00:00
|
|
|
Product = request.registry.get('product.product')
|
|
|
|
product_id = Product.create(request.cr, request.uid, {
|
2014-01-14 14:29:40 +00:00
|
|
|
'name': name, 'public_categ_id': category
|
2013-10-30 13:30:19 +00:00
|
|
|
}, context=request.context)
|
|
|
|
product = Product.browse(request.cr, request.uid, product_id, context=request.context)
|
|
|
|
|
|
|
|
return request.redirect("/shop/product/%s/?enable_editor=1" % product.product_tmpl_id.id)
|
2013-09-04 08:49:58 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/mycart/'], type='http', auth="public", website=True, multilang=True)
|
2013-08-23 14:37:57 +00:00
|
|
|
def mycart(self, **post):
|
2013-11-21 12:09:02 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-08-23 14:37:57 +00:00
|
|
|
prod_obj = request.registry.get('product.product')
|
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
# must have a draft sale order with lines at this point, otherwise reset
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
2013-11-21 12:09:02 +00:00
|
|
|
if order and order.state != 'draft':
|
2014-03-04 14:34:26 +00:00
|
|
|
request.registry['website'].ecommerce_reset(cr, uid, context=context)
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.redirect('/shop/')
|
|
|
|
|
2013-12-01 00:23:55 +00:00
|
|
|
self.get_pricelist()
|
2013-08-23 14:37:57 +00:00
|
|
|
|
|
|
|
suggested_ids = []
|
2013-10-02 16:28:10 +00:00
|
|
|
product_ids = []
|
2013-08-23 14:37:57 +00:00
|
|
|
if order:
|
|
|
|
for line in order.order_line:
|
2014-01-30 23:32:40 +00:00
|
|
|
suggested_ids += [p.id for p in line.product_id and line.product_id.accessory_product_ids or []]
|
2013-10-02 16:28:10 +00:00
|
|
|
product_ids.append(line.product_id.id)
|
|
|
|
suggested_ids = list(set(suggested_ids) - set(product_ids))
|
|
|
|
if suggested_ids:
|
2013-11-21 12:09:02 +00:00
|
|
|
suggested_ids = prod_obj.search(cr, uid, [('id', 'in', suggested_ids)], context=context)
|
2013-09-24 12:19:44 +00:00
|
|
|
|
2013-08-23 14:37:57 +00:00
|
|
|
# select 3 random products
|
|
|
|
suggested_products = []
|
|
|
|
while len(suggested_products) < 3 and suggested_ids:
|
|
|
|
index = random.randrange(0, len(suggested_ids))
|
|
|
|
suggested_products.append(suggested_ids.pop(index))
|
|
|
|
|
2014-01-23 10:47:05 +00:00
|
|
|
context = dict(context or {}, pricelist=request.registry['website'].ecommerce_get_pricelist_id(cr, uid, None, context=context))
|
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-09-30 22:51:05 +00:00
|
|
|
'int': int,
|
2013-11-21 12:09:02 +00:00
|
|
|
'suggested_products': prod_obj.browse(cr, uid, suggested_products, context),
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_sale.mycart", values)
|
2013-08-23 14:37:57 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/add_cart/'], type='http', auth="public", methods=['POST'], website=True, multilang=True)
|
2013-12-13 16:16:42 +00:00
|
|
|
def add_cart(self, product_id, remove=None, **kw):
|
2014-01-17 17:38:36 +00:00
|
|
|
request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
|
|
|
|
product_id=int(product_id),
|
2014-04-08 16:24:32 +00:00
|
|
|
number=float(kw.get('number',1)),
|
|
|
|
set_number=float(kw.get('set_number',-1)),
|
2014-01-17 17:38:36 +00:00
|
|
|
context=request.context)
|
2013-12-13 16:16:42 +00:00
|
|
|
return request.redirect("/shop/mycart/")
|
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/change_cart/<int:order_line_id>/'], type='http', auth="public", website=True, multilang=True)
|
2014-01-07 13:59:33 +00:00
|
|
|
def add_cart_order_line(self, order_line_id=None, remove=None, **kw):
|
2014-01-17 17:38:36 +00:00
|
|
|
request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
|
|
|
|
order_line_id=order_line_id, number=(remove and -1 or 1),
|
|
|
|
context=request.context)
|
2013-09-26 14:54:48 +00:00
|
|
|
return request.redirect("/shop/mycart/")
|
2013-09-26 14:07:50 +00:00
|
|
|
|
2014-01-28 11:31:01 +00:00
|
|
|
@http.route(['/shop/add_cart_json/'], type='json', auth="public", website=True, multilang=True)
|
2013-09-26 14:07:50 +00:00
|
|
|
def add_cart_json(self, product_id=None, order_line_id=None, remove=None):
|
2014-01-17 17:38:36 +00:00
|
|
|
quantity = request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
|
|
|
|
product_id=product_id, order_line_id=order_line_id, number=(remove and -1 or 1),
|
|
|
|
context=request.context)
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
2013-11-26 09:43:20 +00:00
|
|
|
return [quantity,
|
2013-12-19 10:37:32 +00:00
|
|
|
order.get_number_of_products(),
|
2013-11-26 09:43:20 +00:00
|
|
|
order.amount_total,
|
|
|
|
request.website._render("website_sale.total", {'website_sale_order': order})]
|
2013-09-26 14:07:50 +00:00
|
|
|
|
2014-01-28 11:31:01 +00:00
|
|
|
@http.route(['/shop/set_cart_json/'], type='json', auth="public")
|
2013-09-26 14:07:50 +00:00
|
|
|
def set_cart_json(self, path=None, product_id=None, order_line_id=None, set_number=0, json=None):
|
2014-01-28 11:31:01 +00:00
|
|
|
quantity = request.registry['website']._ecommerce_add_product_to_cart(request.cr, request.uid,
|
2014-01-17 17:38:36 +00:00
|
|
|
product_id=product_id, order_line_id=order_line_id, set_number=set_number,
|
|
|
|
context=request.context)
|
2014-04-10 11:39:52 +00:00
|
|
|
order = self.get_order()
|
|
|
|
return [quantity,
|
|
|
|
order.get_number_of_products()]
|
|
|
|
|
2014-01-28 11:31:01 +00:00
|
|
|
return quantity
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/checkout/'], type='http', auth="public", website=True, multilang=True)
|
2013-07-24 09:42:18 +00:00
|
|
|
def checkout(self, **post):
|
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
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
2013-09-24 12:42:53 +00:00
|
|
|
if not order or order.state != 'draft' or not order.order_line:
|
2013-12-18 13:21:50 +00:00
|
|
|
request.registry['website'].ecommerce_reset(cr, uid, context=context)
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.redirect('/shop/')
|
|
|
|
# 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
|
|
|
|
2013-11-25 16:03:58 +00:00
|
|
|
self.get_pricelist()
|
2013-07-24 09:42:18 +00:00
|
|
|
|
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')
|
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)
|
|
|
|
state_orm = registry.get('res.country.state')
|
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)
|
2013-07-02 13:29:40 +00:00
|
|
|
|
2013-11-13 18:43:03 +00:00
|
|
|
info = CheckoutInfo()
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-11-13 18:43:03 +00:00
|
|
|
'countries': countries,
|
|
|
|
'states': states,
|
|
|
|
'checkout': info.empty(),
|
|
|
|
'shipping': post.get("shipping_different"),
|
|
|
|
'error': {},
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-11-13 18:43:03 +00:00
|
|
|
checkout = values['checkout']
|
|
|
|
|
2013-12-03 15:44:22 +00:00
|
|
|
partner = None
|
|
|
|
public_id = request.registry['website'].get_public_user(cr, uid, context)
|
2014-02-27 16:40:00 +00:00
|
|
|
if request.uid != public_id:
|
2013-11-13 18:43:03 +00:00
|
|
|
partner = orm_user.browse(cr, uid, uid, context).partner_id
|
2014-01-28 14:52:46 +00:00
|
|
|
elif order.partner_id:
|
2014-02-27 16:40:00 +00:00
|
|
|
public_partner = orm_user.browse(cr, SUPERUSER_ID, public_id, context=context).partner_id.id
|
|
|
|
if public_partner != order.partner_id.id:
|
2014-01-28 14:52:46 +00:00
|
|
|
partner = orm_partner.browse(cr, SUPERUSER_ID, order.partner_id.id, context)
|
2013-12-03 15:44:22 +00:00
|
|
|
|
|
|
|
if partner:
|
2013-11-13 18:43:03 +00:00
|
|
|
partner_info = info.from_partner(partner)
|
|
|
|
checkout.update(partner_info)
|
2013-12-18 13:21:50 +00:00
|
|
|
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, [("parent_id", "=", partner.id), ('type', "=", 'delivery')], limit=1, context=context)
|
2013-07-24 09:42:18 +00:00
|
|
|
if shipping_ids:
|
2013-11-13 18:43:03 +00:00
|
|
|
values['shipping'] = "true"
|
2013-12-03 15:44:22 +00:00
|
|
|
shipping_partner = orm_partner.browse(cr, SUPERUSER_ID, shipping_ids[0], context)
|
2013-12-18 13:21:50 +00:00
|
|
|
checkout.update(info.from_partner(shipping_partner, address_type='shipping'))
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_sale.checkout", values)
|
2013-07-02 13:29:40 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/confirm_order/'], type='http', auth="public", website=True, multilang=True)
|
2013-07-24 09:42:18 +00:00
|
|
|
def confirm_order(self, **post):
|
2013-11-13 20:27:32 +00:00
|
|
|
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
|
2013-12-03 15:44:22 +00:00
|
|
|
order_line_obj = request.registry.get('sale.order')
|
2013-11-13 18:43:03 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
# must have a draft sale order with lines at this point, otherwise redirect to shop
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
2013-11-13 18:43:03 +00:00
|
|
|
if not order or order.state != 'draft' or not order.order_line:
|
2013-12-18 13:21:50 +00:00
|
|
|
request.registry['website'].ecommerce_reset(cr, uid, context=context)
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.redirect('/shop/')
|
|
|
|
# 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-07-23 13:39:23 +00:00
|
|
|
|
2014-01-28 14:52:46 +00:00
|
|
|
orm_partner = registry.get('res.partner')
|
2013-11-13 18:43:03 +00:00
|
|
|
orm_user = registry.get('res.users')
|
|
|
|
orm_country = registry.get('res.country')
|
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)
|
|
|
|
orm_state = registry.get('res.country.state')
|
2013-11-21 14:16:47 +00:00
|
|
|
states_ids = orm_state.search(cr, SUPERUSER_ID, [], context=context)
|
2013-11-13 18:43:03 +00:00
|
|
|
states = orm_state.browse(cr, SUPERUSER_ID, states_ids, context)
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2013-11-13 18:43:03 +00:00
|
|
|
info = CheckoutInfo()
|
|
|
|
values = {
|
|
|
|
'countries': countries,
|
|
|
|
'states': states,
|
|
|
|
'checkout': info.empty(),
|
|
|
|
'shipping': post.get("shipping_different"),
|
|
|
|
'error': {},
|
|
|
|
}
|
|
|
|
checkout = values['checkout']
|
|
|
|
checkout.update(post)
|
|
|
|
error = values['error']
|
|
|
|
|
|
|
|
for field_name in info.mandatory_billing_fields:
|
|
|
|
if not checkout[field_name]:
|
|
|
|
error[field_name] = 'missing'
|
|
|
|
if post.get("shipping_different"):
|
|
|
|
for field_name in info.mandatory_shipping_fields:
|
|
|
|
if not checkout[field_name]:
|
|
|
|
error[field_name] = 'missing'
|
2013-08-08 14:23:41 +00:00
|
|
|
if error:
|
2013-11-13 18:43:03 +00:00
|
|
|
return request.website.render("website_sale.checkout", values)
|
2013-07-23 13:39:23 +00:00
|
|
|
|
2014-01-15 11:05:47 +00:00
|
|
|
billing_info = dict((k, v) for k,v in checkout.items() if "shipping_" not in k and k != "company")
|
2013-10-08 15:22:33 +00:00
|
|
|
|
2014-01-28 15:21:36 +00:00
|
|
|
partner_id = None
|
2014-01-28 14:52:46 +00:00
|
|
|
public_id = request.registry['website'].get_public_user(cr, uid, context)
|
|
|
|
if request.uid != public_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-02-27 16:40:00 +00:00
|
|
|
public_partner = orm_user.browse(cr, SUPERUSER_ID, public_id, context=context).partner_id.id
|
|
|
|
if public_partner != order.partner_id.id:
|
2014-01-28 14:52:46 +00:00
|
|
|
partner_id = order.partner_id.id
|
2014-01-28 15:21:36 +00:00
|
|
|
|
2014-01-28 14:52:46 +00:00
|
|
|
if partner_id:
|
|
|
|
orm_partner.write(cr, SUPERUSER_ID, [partner_id], billing_info, context=context)
|
2013-11-13 18:43:03 +00:00
|
|
|
else:
|
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
|
|
|
|
2013-07-23 16:13:19 +00:00
|
|
|
shipping_id = None
|
2013-09-05 09:59:33 +00:00
|
|
|
if post.get('shipping_different'):
|
2013-11-13 18:43:03 +00:00
|
|
|
shipping_info = {
|
2013-07-23 15:16:07 +00:00
|
|
|
'phone': post['shipping_phone'],
|
2013-07-23 13:39:23 +00:00
|
|
|
'zip': post['shipping_zip'],
|
2014-04-10 10:58:04 +00:00
|
|
|
'street': checkout['company'],
|
|
|
|
'street2': post['shipping_street'],
|
2013-07-23 13:39:23 +00:00
|
|
|
'city': post['shipping_city'],
|
|
|
|
'name': post['shipping_name'],
|
2013-11-15 13:34:14 +00:00
|
|
|
'email': post['email'],
|
2013-07-23 16:13:19 +00:00
|
|
|
'type': 'delivery',
|
|
|
|
'parent_id': partner_id,
|
2013-07-23 15:16:07 +00:00
|
|
|
'country_id': post['shipping_country_id'],
|
2013-07-23 15:42:11 +00:00
|
|
|
'state_id': post['shipping_state_id'],
|
2013-07-23 13:39:23 +00:00
|
|
|
}
|
2013-12-19 10:37:32 +00:00
|
|
|
domain = [(key, '_id' in key and '=' or 'ilike', '_id' in key and value and int(value) or value)
|
|
|
|
for key, value in shipping_info.items() if key in info.mandatory_billing_fields + ["type", "parent_id"]]
|
2013-09-05 09:59:33 +00:00
|
|
|
|
2014-01-28 14:52:46 +00:00
|
|
|
shipping_ids = orm_partner.search(cr, SUPERUSER_ID, domain, context=context)
|
2013-07-24 09:42:18 +00:00
|
|
|
if shipping_ids:
|
|
|
|
shipping_id = shipping_ids[0]
|
2014-01-28 14:52:46 +00:00
|
|
|
orm_partner.write(cr, SUPERUSER_ID, [shipping_id], shipping_info, context)
|
2013-07-24 09:42:18 +00:00
|
|
|
else:
|
2014-01-28 14:52:46 +00:00
|
|
|
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,
|
2013-10-08 15:22:33 +00:00
|
|
|
'message_follower_ids': [(4, partner_id)],
|
2013-07-23 16:13:19 +00:00
|
|
|
'partner_invoice_id': partner_id,
|
|
|
|
'partner_shipping_id': shipping_id or partner_id
|
2013-07-24 09:42:18 +00:00
|
|
|
}
|
2013-12-24 14:58:45 +00:00
|
|
|
order_info.update(registry.get('sale.order').onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
|
2014-01-24 11:42:24 +00:00
|
|
|
order_info.pop('user_id')
|
2013-12-03 15:44:22 +00:00
|
|
|
|
|
|
|
order_line_obj.write(cr, SUPERUSER_ID, [order.id], order_info, context=context)
|
2013-07-23 16:13:19 +00:00
|
|
|
|
2013-09-26 14:54:48 +00:00
|
|
|
return request.redirect("/shop/payment/")
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/payment/'], type='http', auth="public", website=True, multilang=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-04-02 14:45:20 +00:00
|
|
|
sale_order_obj = request.registry['sale.order']
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2013-11-19 16:01:29 +00:00
|
|
|
# if no sale order at this stage: back to checkout beginning
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
2014-01-10 10:28:58 +00:00
|
|
|
if not order or order.state != 'draft' or not order.order_line:
|
2013-12-18 13:21:50 +00:00
|
|
|
request.registry['website'].ecommerce_reset(cr, uid, context=context)
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.redirect("/shop/")
|
2013-11-19 16:01:29 +00:00
|
|
|
# alread a transaction: forward to confirmation
|
|
|
|
tx = context.get('website_sale_transaction')
|
2014-01-10 10:28:58 +00:00
|
|
|
if tx and tx.state != 'draft':
|
2013-12-17 15:26:05 +00:00
|
|
|
return request.redirect('/shop/confirmation/%s' % order.id)
|
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
|
|
|
|
2014-04-04 10:25:12 +00:00
|
|
|
values = {}
|
|
|
|
values['website_sale_order'] = values['order'] = sale_order_obj.browse(cr, SUPERUSER_ID, order.id, context=context)
|
2014-04-02 14:45:20 +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=context))
|
2013-07-24 09:42:18 +00:00
|
|
|
|
2014-04-02 14:45:20 +00:00
|
|
|
if not values['errors']:
|
|
|
|
# fetch all registered payment means
|
|
|
|
if tx:
|
|
|
|
acquirer_ids = [tx.acquirer_id.id]
|
|
|
|
else:
|
|
|
|
acquirer_ids = payment_obj.search(cr, SUPERUSER_ID, [('website_published', '=', True), '|', ('company_id', '=', order.company_id.id), ('company_id', '=', False)], context=context)
|
|
|
|
values['acquirers'] = 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']:
|
|
|
|
render_ctx['tx_url'] = '/shop/payment/transaction/%s' % acquirer.id
|
|
|
|
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-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/payment/transaction/<int:acquirer_id>'],
|
|
|
|
type='http', methods=['POST'], auth="public", website=True)
|
2013-11-21 12:09:02 +00:00
|
|
|
def payment_transaction(self, acquirer_id, **post):
|
2013-11-15 13:34:14 +00:00
|
|
|
""" Hook method that creates a payment.transaction and redirect to the
|
|
|
|
acquirer, using post values to re-create the post action.
|
|
|
|
|
|
|
|
:param int acquirer_id: id of a payment.acquirer record. If not set the
|
|
|
|
user is redirected to the checkout page
|
2013-11-19 16:01:29 +00:00
|
|
|
:param dict post: should coutain all post data for the acquirer
|
2013-11-15 13:34:14 +00:00
|
|
|
"""
|
|
|
|
# @TDEFIXME: don't know why we received those data, but should not be send to the acquirer
|
|
|
|
post.pop('submit.x', None)
|
|
|
|
post.pop('submit.y', None)
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
payment_obj = request.registry.get('payment.acquirer')
|
|
|
|
transaction_obj = request.registry.get('payment.transaction')
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
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:
|
2013-11-15 13:34:14 +00:00
|
|
|
return request.redirect("/shop/checkout/")
|
|
|
|
|
|
|
|
# find an already existing transaction
|
2013-11-19 16:01:29 +00:00
|
|
|
tx = context.get('website_sale_transaction')
|
|
|
|
if not tx:
|
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,
|
|
|
|
'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)
|
2013-11-19 16:01:29 +00:00
|
|
|
request.httprequest.session['website_sale_transaction_id'] = tx_id
|
|
|
|
elif tx and tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ?
|
|
|
|
tx.write({
|
|
|
|
'acquirer_id': acquirer_id,
|
|
|
|
})
|
2013-11-15 13:34:14 +00:00
|
|
|
|
|
|
|
acquirer_form_post_url = payment_obj.get_form_action_url(cr, uid, acquirer_id, context=context)
|
2014-01-29 12:13:16 +00:00
|
|
|
acquirer_total_url = '%s?%s' % (acquirer_form_post_url, werkzeug.url_encode(post))
|
2013-11-15 13:34:14 +00:00
|
|
|
return request.redirect(acquirer_total_url)
|
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route('/shop/payment/get_status/<int:sale_order_id>', type='json', auth="public", website=True, multilang=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)
|
|
|
|
assert order.website_session_id == request.httprequest.session['website_session_id']
|
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if not order:
|
|
|
|
return {
|
|
|
|
'state': 'error',
|
2014-01-24 14:43:55 +00:00
|
|
|
'message': '<p>There seems to be an error with your request.</p>',
|
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(
|
|
|
|
cr, uid, [
|
|
|
|
'|', ('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',
|
|
|
|
'message': '<p>There seems to be an error with your request.</p>',
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
state = 'done'
|
|
|
|
message = ""
|
|
|
|
validation = None
|
2014-01-24 14:43:55 +00:00
|
|
|
else:
|
2014-01-28 16:38:15 +00:00
|
|
|
tx = request.registry['payment.transaction'].browse(cr, uid, tx_ids[0], context=context)
|
|
|
|
state = tx.state
|
|
|
|
if state == 'done':
|
|
|
|
message = '<p>Your payment has been received.</p>'
|
|
|
|
elif state == 'cancel':
|
|
|
|
message = '<p>The payment seems to have been canceled.</p>'
|
|
|
|
elif state == 'pending' and tx.acquirer_id.validation == 'manual':
|
|
|
|
message = '<p>Your transaction is waiting confirmation.</p>'
|
|
|
|
message += tx.acquirer_id.post_msg
|
|
|
|
else:
|
|
|
|
message = '<p>Your transaction is waiting confirmation.</p>'
|
|
|
|
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-01-20 15:37:33 +00:00
|
|
|
@http.route('/shop/payment/validate/', type='http', auth="public", website=True, multilang=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
|
|
|
|
sale_order_obj = request.registry['sale.order']
|
2014-03-04 15:44:33 +00:00
|
|
|
email_act = None
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
if transaction_id is None:
|
|
|
|
tx = context.get('website_sale_transaction')
|
|
|
|
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:
|
2013-12-18 13:21:50 +00:00
|
|
|
order = self.get_order()
|
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)
|
|
|
|
assert order.website_session_id == request.httprequest.session['website_session_id']
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2014-02-28 12:34:14 +00:00
|
|
|
if not order:
|
2014-01-28 16:38:15 +00:00
|
|
|
return request.redirect('/shop/')
|
2014-02-28 12:34:14 +00:00
|
|
|
elif order.amount_total and not tx:
|
|
|
|
return request.redirect('/shop/mycart')
|
2014-01-28 16:38:15 +00:00
|
|
|
|
|
|
|
if not order.amount_total or tx.state == 'done':
|
2013-11-20 14:03:21 +00:00
|
|
|
# confirm the quotation
|
|
|
|
sale_order_obj.action_button_confirm(cr, SUPERUSER_ID, [order.id], context=request.context)
|
|
|
|
# send by email
|
2014-03-04 15:44:33 +00:00
|
|
|
email_act = sale_order_obj.action_quotation_send(cr, SUPERUSER_ID, [order.id], context=request.context)
|
2013-11-20 14:03:21 +00:00
|
|
|
elif tx.state == 'pending':
|
|
|
|
# send by email
|
2014-03-04 15:44:33 +00:00
|
|
|
email_act = sale_order_obj.action_quotation_send(cr, SUPERUSER_ID, [order.id], context=request.context)
|
2013-11-20 14:03:21 +00:00
|
|
|
elif tx.state == 'cancel':
|
|
|
|
# cancel the quotation
|
|
|
|
sale_order_obj.action_cancel(cr, SUPERUSER_ID, [order.id], context=request.context)
|
|
|
|
|
2014-03-04 15:44:33 +00:00
|
|
|
# send the email
|
|
|
|
if email_act and email_act.get('context'):
|
|
|
|
composer_values = {}
|
|
|
|
email_ctx = email_act['context']
|
|
|
|
public_id = request.registry['website'].get_public_user(cr, uid, context)
|
|
|
|
if uid == public_id:
|
|
|
|
composer_values['email_from'] = request.registry['res.users'].browse(cr, SUPERUSER_ID, public_id, context=context).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
|
2013-12-18 13:21:50 +00:00
|
|
|
request.registry['website'].ecommerce_reset(cr, uid, context=context)
|
2013-11-20 14:03:21 +00:00
|
|
|
|
2013-11-21 12:09:02 +00:00
|
|
|
return request.redirect('/shop/confirmation/%s' % order.id)
|
2013-10-17 11:00:46 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/shop/confirmation/<int:sale_order_id>'], type='http', auth="public", website=True, multilang=True)
|
2014-01-07 13:59:33 +00:00
|
|
|
def payment_confirmation(self, sale_order_id, **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-01-07 13:59:33 +00:00
|
|
|
order = request.registry['sale.order'].browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
|
|
|
assert order.website_session_id == request.httprequest.session['website_session_id']
|
|
|
|
|
2014-01-22 15:18:52 +00:00
|
|
|
request.registry['website']._ecommerce_change_pricelist(cr, uid, None, context=context or {})
|
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-28 11:31:01 +00:00
|
|
|
@http.route(['/shop/change_sequence/'], type='json', auth="public")
|
2014-01-10 11:24:42 +00:00
|
|
|
def change_sequence(self, id, sequence):
|
2013-09-26 11:18:54 +00:00
|
|
|
product_obj = request.registry.get('product.template')
|
2014-01-10 11:24:42 +00:00
|
|
|
if sequence == "top":
|
2013-10-02 07:12:39 +00:00
|
|
|
product_obj.set_sequence_top(request.cr, request.uid, [id], context=request.context)
|
2014-01-10 11:24:42 +00:00
|
|
|
elif sequence == "bottom":
|
2013-10-02 07:12:39 +00:00
|
|
|
product_obj.set_sequence_bottom(request.cr, request.uid, [id], context=request.context)
|
2014-01-10 11:24:42 +00:00
|
|
|
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)
|
2013-09-26 11:18:54 +00:00
|
|
|
|
2014-01-28 11:31:01 +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-01-28 11:31:01 +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
|
|
|
|
2013-06-28 16:28:04 +00:00
|
|
|
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|