2013-06-28 16:28:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import openerp
|
2013-07-02 12:06:25 +00:00
|
|
|
import simplejson
|
2013-06-28 16:28:04 +00:00
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2013-07-02 13:29:40 +00:00
|
|
|
from openerp.addons.website.controllers.main import template_values
|
2013-06-28 16:28:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Ecommerce(http.Controller):
|
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
def get_cr_uid(self):
|
2013-07-02 09:41:38 +00:00
|
|
|
cr = request.cr
|
2013-07-02 13:29:40 +00:00
|
|
|
uid = request.session._uid or openerp.SUPERUSER_ID
|
2013-07-10 15:03:39 +00:00
|
|
|
# todo => admin => request.uid and login => request.session.uid ; auth="admin"
|
2013-07-10 14:29:08 +00:00
|
|
|
if uid != 1:
|
|
|
|
request.httprequest.session['ecommerce_partner_id'] = False
|
|
|
|
partner_id = None
|
|
|
|
else:
|
|
|
|
partner_id = request.httprequest.session.get('ecommerce_partner_id', False)
|
|
|
|
|
2013-07-02 15:21:41 +00:00
|
|
|
if partner_id and not request.registry.get('res.partner').search(cr, uid, [('id', '=', partner_id)]):
|
|
|
|
partner_id = None
|
2013-07-02 13:29:40 +00:00
|
|
|
if not partner_id:
|
2013-07-02 12:06:25 +00:00
|
|
|
partner_id = request.registry.get('res.users').browse(cr, uid, uid).partner_id.id
|
2013-07-02 09:41:38 +00:00
|
|
|
return (cr, uid, partner_id)
|
2013-07-01 09:58:43 +00:00
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
def get_values(self):
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
|
|
|
order_obj = request.registry.get('sale.order')
|
2013-07-02 12:06:25 +00:00
|
|
|
category_obj = request.registry.get('pos.category')
|
2013-07-02 09:41:38 +00:00
|
|
|
context = {}
|
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
order_id = request.httprequest.session.get('ecommerce_order')
|
2013-07-02 12:06:25 +00:00
|
|
|
# check if order allready exists
|
|
|
|
try:
|
|
|
|
order_obj.browse(cr, uid, order_id).pricelist_id
|
|
|
|
except:
|
|
|
|
order_id = None
|
|
|
|
|
|
|
|
if not order_id:
|
2013-07-02 09:41:38 +00:00
|
|
|
fields = [k for k, v in order_obj._columns.items()]
|
|
|
|
order_value = order_obj.default_get(cr, uid, fields, context=context)
|
2013-07-02 15:21:41 +00:00
|
|
|
order_value['partner_id'] = partner_id
|
2013-07-02 09:41:38 +00:00
|
|
|
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)
|
2013-07-02 13:29:40 +00:00
|
|
|
request.httprequest.session['ecommerce_order'] = order_id
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2013-07-10 14:29:08 +00:00
|
|
|
category_ids = category_obj.search(cr, openerp.SUPERUSER_ID, [('parent_id', '=', False)])
|
2013-07-01 09:58:43 +00:00
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
values = template_values()
|
|
|
|
values.update({
|
2013-07-02 12:06:25 +00:00
|
|
|
'temp': 0,
|
2013-07-02 13:29:40 +00:00
|
|
|
'res_company': request.registry['res.company'].browse(request.cr, uid, 1, context=context),
|
2013-07-02 12:06:25 +00:00
|
|
|
'order': order_obj.browse(cr, uid, order_id),
|
2013-07-10 14:29:08 +00:00
|
|
|
'categories': category_obj.browse(cr, openerp.SUPERUSER_ID, category_ids),
|
2013-07-02 13:29:40 +00:00
|
|
|
})
|
2013-07-01 16:12:47 +00:00
|
|
|
return values
|
|
|
|
|
|
|
|
@http.route(['/shop', '/shop/category/<cat_id>'], type='http', auth="db")
|
2013-07-10 15:03:39 +00:00
|
|
|
def category(self, cat_id=0, offset=0, **post):
|
2013-07-01 16:12:47 +00:00
|
|
|
values = self.get_values()
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2013-07-10 15:03:39 +00:00
|
|
|
domain = []
|
|
|
|
if post.get("search"):
|
|
|
|
domain += ['|', '|', ('name', 'ilike', "%%%s%%" % post.get("search")), ('description', 'ilike', "%%%s%%" % post.get("search")), ('pos_categ_id.name', 'ilike', "%%%s%%" % post.get("search"))]
|
|
|
|
if cat_id:
|
|
|
|
cat_id = cat_id and int(cat_id) or 0
|
|
|
|
domain = [('pos_categ_id.id', 'child_of', cat_id)] + domain
|
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
category_obj = request.registry.get('pos.category')
|
|
|
|
product_obj = request.registry.get('product.product')
|
2013-07-10 14:29:08 +00:00
|
|
|
category_ids = category_obj.search(cr, openerp.SUPERUSER_ID, [('parent_id', '=', False)])
|
2013-07-10 15:03:39 +00:00
|
|
|
product_ids = product_obj.search(cr, openerp.SUPERUSER_ID, domain or [(1, '=', 1)], limit=20, offset=offset)
|
2013-07-01 16:12:47 +00:00
|
|
|
|
|
|
|
values.update({
|
|
|
|
'current_category': cat_id,
|
2013-07-10 14:29:08 +00:00
|
|
|
'categories': category_obj.browse(cr, openerp.SUPERUSER_ID, category_ids),
|
|
|
|
'products': product_obj.browse(cr, openerp.SUPERUSER_ID, product_ids),
|
2013-07-10 15:03:39 +00:00
|
|
|
'search': post.get("search"),
|
2013-07-01 16:12:47 +00:00
|
|
|
})
|
2013-07-02 20:48:49 +00:00
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "website_sale.products", 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()
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
2013-07-01 09:58:43 +00:00
|
|
|
|
|
|
|
product_id = product_id and int(product_id) or 0
|
|
|
|
product_obj = request.registry.get('product.product')
|
|
|
|
|
2013-07-01 16:12:47 +00:00
|
|
|
values.update({
|
2013-07-10 14:29:08 +00:00
|
|
|
'product': product_obj.browse(cr, openerp.SUPERUSER_ID, product_id),
|
2013-07-01 16:12:47 +00:00
|
|
|
})
|
2013-07-02 15:21:41 +00:00
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "website_sale.product", values)
|
2013-07-01 16:12:47 +00:00
|
|
|
return html
|
|
|
|
|
|
|
|
@http.route(['/shop/my_cart'], type='http', auth="db")
|
2013-07-02 12:06:25 +00:00
|
|
|
def my_cart(self, offset=0):
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
2013-07-02 15:21:41 +00:00
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "website_sale.my_cart", self.get_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):
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
2013-07-02 12:06:25 +00:00
|
|
|
values = self.get_values()
|
2013-07-01 16:12:47 +00:00
|
|
|
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
|
2013-07-02 12:06:25 +00:00
|
|
|
order = values['order']
|
|
|
|
|
|
|
|
quantity = 0
|
2013-07-01 16:12:47 +00:00
|
|
|
|
|
|
|
# values initialisation
|
2013-07-02 12:06:25 +00:00
|
|
|
order_line_ids = order_line_obj.search(cr, uid, [('order_id', '=', order.id), ('product_id', '=', product_id)], context=context)
|
2013-07-01 16:12:47 +00:00
|
|
|
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)
|
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
|
2013-07-02 12:06:25 +00:00
|
|
|
values['order_id'] = order.id
|
2013-07-01 16:12:47 +00:00
|
|
|
|
|
|
|
# change and record value
|
2013-07-02 12:06:25 +00:00
|
|
|
if quantity:
|
|
|
|
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)
|
|
|
|
|
2013-07-02 15:21:41 +00:00
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "website_sale.total", self.get_values())
|
2013-07-01 16:12:47 +00:00
|
|
|
|
2013-07-02 12:06:25 +00:00
|
|
|
return simplejson.dumps({"quantity": quantity, "totalHTML": html})
|
2013-07-01 16:12:47 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
@http.route(['/shop/customer'], type='http', auth="db")
|
|
|
|
def customer(self, *arg, **post):
|
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
|
|
|
values = self.get_values()
|
|
|
|
partner_obj = request.registry.get('res.partner')
|
2013-07-10 14:29:08 +00:00
|
|
|
user_obj = request.registry.get('res.users')
|
2013-07-02 13:29:40 +00:00
|
|
|
|
|
|
|
values['partner'] = False
|
2013-07-10 14:29:08 +00:00
|
|
|
partner_id = uid != 1 and partner_id or request.httprequest.session.get('ecommerce_partner_id')
|
|
|
|
|
|
|
|
if post.get("login"):
|
|
|
|
user_id = user_obj.login(cr, post.get("login"), post.get("password"))
|
|
|
|
partner_id = user_obj.browse(cr, uid, user_id).partner_id.id
|
|
|
|
request.httprequest.session['ecommerce_partner_id'] = partner_id
|
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
if partner_id:
|
|
|
|
values['partner'] = partner_obj.browse(cr, uid, partner_id)
|
|
|
|
|
2013-07-10 14:29:08 +00:00
|
|
|
return request.registry.get("ir.ui.view").render(cr, uid, "website_sale.customer", values)
|
2013-07-02 13:29:40 +00:00
|
|
|
|
2013-07-02 12:06:25 +00:00
|
|
|
@http.route(['/shop/confirm_cart'], type='http', auth="db")
|
|
|
|
def confirm_cart(self, *arg, **post):
|
2013-07-02 09:41:38 +00:00
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
|
|
|
values = self.get_values()
|
2013-07-02 13:29:40 +00:00
|
|
|
partner_obj = request.registry.get('res.partner')
|
|
|
|
|
|
|
|
values['partner'] = False
|
2013-07-10 14:29:08 +00:00
|
|
|
partner_id = uid != 1 and partner_id or request.httprequest.session.get('ecommerce_partner_id')
|
2013-07-02 12:06:25 +00:00
|
|
|
if post:
|
2013-07-10 14:29:08 +00:00
|
|
|
post['country_id'] = (request.registry.get('res.country').search(cr, openerp.SUPERUSER_ID, [('name', 'ilike', post.pop('country'))]) + [None])[0]
|
2013-07-02 13:29:40 +00:00
|
|
|
post['state_id'] = (request.registry.get('res.country.state').search(cr, uid, [('name', 'ilike', post.pop('state'))]) + [None])[0]
|
2013-07-02 12:06:25 +00:00
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
if partner_id:
|
|
|
|
partner_obj.write(cr, uid, [partner_id], post)
|
|
|
|
else:
|
|
|
|
partner_id = partner_obj.create(cr, uid, post)
|
|
|
|
values['order'].write({'partner_id': partner_id})
|
|
|
|
request.httprequest.session['ecommerce_partner_id'] = partner_id
|
2013-07-02 12:06:25 +00:00
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
values['partner'] = partner_obj.browse(cr, uid, partner_id)
|
2013-07-10 14:29:08 +00:00
|
|
|
return request.registry.get("ir.ui.view").render(cr, uid, "website_sale.order", values)
|
2013-07-02 12:06:25 +00:00
|
|
|
|
2013-07-02 13:29:40 +00:00
|
|
|
@http.route(['/shop/confirm_order'], type='http', auth="db")
|
|
|
|
def confirm_order(self, *arg, **post):
|
|
|
|
cr, uid, partner_id = self.get_cr_uid()
|
2013-07-10 14:29:08 +00:00
|
|
|
if uid == 1:
|
|
|
|
return customer()
|
2013-07-02 13:29:40 +00:00
|
|
|
values = self.get_values()
|
|
|
|
values['order'].write({'state': 'progress'})
|
|
|
|
values['partner'] = request.registry.get('res.partner').browse(cr, uid, partner_id)
|
2013-07-02 15:21:41 +00:00
|
|
|
html = request.registry.get("ir.ui.view").render(cr, uid, "website_sale.thanks", values)
|
2013-07-02 13:29:40 +00:00
|
|
|
request.httprequest.session['ecommerce_order'] = None
|
2013-07-02 09:41:38 +00:00
|
|
|
return html
|
2013-06-28 16:28:04 +00:00
|
|
|
|
|
|
|
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
|