2013-06-28 16:28:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import openerp
|
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
|
|
|
class Ecommerce(http.Controller):
|
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
def get_cr_uid(self):
|
2013-07-01 09:58:43 +00:00
|
|
|
try:
|
|
|
|
request.session.check_security()
|
|
|
|
uid = request.session._uid
|
|
|
|
except http.SessionExpiredException:
|
|
|
|
uid = openerp.SUPERUSER_ID
|
2013-07-01 16:12:47 +00:00
|
|
|
return (request.cr, uid)
|
2013-07-01 09:58:43 +00:00
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
def get_values(self):
|
|
|
|
cr, uid = self.get_cr_uid()
|
|
|
|
|
|
|
|
total_price = 0
|
|
|
|
cart = {}
|
|
|
|
if request.session.get('ecommerce'):
|
|
|
|
order = request.registry.get('sale.order').browse(cr, uid, request.session.get('ecommerce'))
|
|
|
|
total_price = order.amount_total
|
|
|
|
for line in order.order_line:
|
|
|
|
cart[line.product_id.id] = {'product_id': line.product_id.id, 'quantity': int(line.product_uom_qty)}
|
2013-07-01 09:58:43 +00:00
|
|
|
|
|
|
|
values = {
|
|
|
|
'request': request,
|
|
|
|
'registry': request.registry,
|
2013-07-01 16:12:47 +00:00
|
|
|
'cr': cr,
|
2013-07-01 09:58:43 +00:00
|
|
|
'uid': uid,
|
2013-07-01 16:12:47 +00:00
|
|
|
'cart': cart,
|
|
|
|
'total_price': total_price,
|
2013-07-01 09:58:43 +00:00
|
|
|
}
|
2013-07-01 16:12:47 +00:00
|
|
|
return values
|
|
|
|
|
|
|
|
@http.route(['/shop', '/shop/category/<cat_id>'], type='http', auth="db")
|
|
|
|
def category(self, cat_id=0, offset=0):
|
|
|
|
values = self.get_values()
|
|
|
|
cr, uid = self.get_cr_uid()
|
|
|
|
|
|
|
|
cat_id = cat_id and int(cat_id) or 0
|
|
|
|
category_obj = request.registry.get('pos.category')
|
|
|
|
product_obj = request.registry.get('product.product')
|
|
|
|
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
|
|
|
product_ids = product_obj.search(cr, uid, cat_id and [('pos_categ_id.id', 'child_of', cat_id)] or [(1, '=', 1)], limit=20, offset=offset)
|
|
|
|
|
|
|
|
values.update({
|
|
|
|
'current_category': cat_id,
|
|
|
|
'categories': category_obj.browse(cr, uid, category_ids),
|
|
|
|
'products': product_obj.browse(cr, uid, product_ids),
|
|
|
|
})
|
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.categories", values)
|
2013-07-01 09:58:43 +00:00
|
|
|
return html
|
2013-06-28 16:28:04 +00:00
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
@http.route(['/shop/product/<product_id>'], type='http', auth="db")
|
2013-07-01 09:58:43 +00:00
|
|
|
def product(self, cat_id=0, product_id=0, offset=0):
|
2013-07-01 16:12:47 +00:00
|
|
|
values = self.get_values()
|
|
|
|
cr, uid = self.get_cr_uid()
|
2013-07-01 09:58:43 +00:00
|
|
|
|
|
|
|
product_id = product_id and int(product_id) or 0
|
2013-06-28 16:28:04 +00:00
|
|
|
category_obj = request.registry.get('pos.category')
|
2013-07-01 09:58:43 +00:00
|
|
|
product_obj = request.registry.get('product.product')
|
2013-07-01 16:12:47 +00:00
|
|
|
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
2013-07-01 09:58:43 +00:00
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
values.update({
|
|
|
|
'categories': category_obj.browse(cr, uid, category_ids),
|
|
|
|
'product': product_obj.browse(cr, uid, product_id),
|
|
|
|
})
|
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.product", values)
|
|
|
|
return html
|
|
|
|
|
|
|
|
@http.route(['/shop/my_cart'], type='http', auth="db")
|
|
|
|
def my_cart(self, cat_id=0, offset=0):
|
|
|
|
values = self.get_values()
|
|
|
|
cr, uid = self.get_cr_uid()
|
|
|
|
|
|
|
|
cat_id = cat_id and int(cat_id) or 0
|
|
|
|
category_obj = request.registry.get('pos.category')
|
|
|
|
product_obj = request.registry.get('product.product')
|
|
|
|
|
|
|
|
category_ids = category_obj.search(cr, uid, [('parent_id', '=', False)])
|
|
|
|
product_ids = [product for product in values['cart']]
|
|
|
|
|
|
|
|
values.update({
|
|
|
|
'my_cart': True,
|
|
|
|
'categories': category_obj.browse(cr, uid, category_ids),
|
|
|
|
'products': product_obj.browse(cr, uid, product_ids),
|
|
|
|
})
|
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "ecommerce.categories", values)
|
2013-06-28 16:28:04 +00:00
|
|
|
return html
|
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
@http.route(['/shop/add_cart'], type='http', auth="db")
|
|
|
|
def add_cart(self, product_id=0, remove=False):
|
|
|
|
cr, uid = self.get_cr_uid()
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
order_obj = request.registry.get('sale.order')
|
|
|
|
order_line_obj = request.registry.get('sale.order.line')
|
|
|
|
|
|
|
|
product_id = product_id and int(product_id) or 0
|
|
|
|
partner = request.registry.get('res.users').browse(cr, uid, uid, context=context).partner_id
|
|
|
|
|
|
|
|
order_id = request.session.get('ecommerce')
|
|
|
|
|
|
|
|
# find or set order_id
|
|
|
|
if not order_id:
|
|
|
|
fields = [k for k, v in order_obj._columns.items()]
|
|
|
|
order_value = order_obj.default_get(cr, uid, fields, context=context)
|
|
|
|
order_value['partner_id'] = partner.id
|
|
|
|
order_value.update(order_obj.onchange_partner_id(cr, uid, [], uid, context=context)['value'])
|
|
|
|
order_id = order_obj.create(cr, uid, order_value, context=context)
|
|
|
|
request.session['ecommerce'] = order_id
|
|
|
|
order = order_obj.browse(cr, uid, order_id, context=context)
|
|
|
|
|
|
|
|
# values initialisation
|
|
|
|
order_line_ids = order_line_obj.search(cr, uid, [('order_id', '=', order_id), ('product_id', '=', product_id)], context=context)
|
|
|
|
values = {}
|
|
|
|
if order_line_ids:
|
|
|
|
order_line = order_line_obj.read(cr, uid, order_line_ids, [], context=context)[0]
|
|
|
|
quantity = order_line['product_uom_qty'] + (remove and -1 or 1)
|
2013-07-01 16:20:28 +00:00
|
|
|
if quantity <= 0:
|
2013-07-01 16:16:43 +00:00
|
|
|
order_line_obj.unlink(cr, uid, order_line_ids, context=context)
|
|
|
|
return "0"
|
2013-07-01 16:12:47 +00:00
|
|
|
else:
|
|
|
|
fields = [k for k, v in order_line_obj._columns.items()]
|
|
|
|
values = order_line_obj.default_get(cr, uid, fields, context=context)
|
|
|
|
quantity = 1
|
|
|
|
values['product_uom_qty'] = quantity
|
|
|
|
values['product_id'] = product_id
|
|
|
|
values['order_id'] = order_id
|
|
|
|
|
|
|
|
# change and record value
|
|
|
|
pricelist_id = order.pricelist_id and order.pricelist_id.id or False
|
|
|
|
values.update(order_line_obj.product_id_change(cr, uid, [], pricelist_id, product_id, partner_id=partner.id, context=context)['value'])
|
|
|
|
if order_line_ids:
|
|
|
|
order_line_obj.write(cr, uid, order_line_ids, values, context=context)
|
|
|
|
else:
|
|
|
|
order_line_id = order_line_obj.create(cr, uid, values, context=context)
|
|
|
|
order.write({'order_line': [(4, order_line_id)]}, context=context)
|
|
|
|
|
|
|
|
return "%s" % values['product_uom_qty']
|
|
|
|
|
|
|
|
@http.route(['/shop/remove_cart'], type='http', auth="db")
|
|
|
|
def remove_cart(self, product_id=0):
|
2013-07-01 16:16:43 +00:00
|
|
|
return self.add_cart(product_id=product_id, remove=True)
|
2013-07-01 16:12:47 +00:00
|
|
|
|
|
|
|
@http.route(['/shop/confirm_cart'], type='http', auth="db")
|
|
|
|
def confirm_cart(self):
|
|
|
|
return ""
|
2013-06-28 16:28:04 +00:00
|
|
|
|
|
|
|
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|